【发布时间】:2015-08-18 10:42:19
【问题描述】:
我在 MVC 4 中工作,想在视图中绑定我使用 Tuple 的两个模型,现在我想从两个不同 Div 中的两个模型中获取值,但我收到以下错误:
here is my Controller code:
public ActionResult Edit()
{
CygnusInternalResponseViewModel result = new CygnusInternalResponseViewModel();
result = new Logic(CApplicationId, CurrentCompanyId).GetProducts();
if (result.Success)
return View(result.Model);
ProductOrderViewModel objparent = new ProductOrderViewModel();
List<ProductOrderViewModel> viewmodel = new List<ProductOrderViewModel>();
viewmodel.Add(objparent);
return View(viewmodel.AsEnumerable());
// return View();
}
这是我的型号代码:
public class ProductOrderViewModel
{
public List<ProductViewModel> Products { get; set; }
public List<OrderViewModel> Orders { get; set; }
}
请指导我解决这个问题..以下是我的代码
@model Cygnus.Global.ViewModels.ProductOrderViewModel
@using Cygnus.Global.ViewHelpers;
@using Cygnus.Global.ViewModels;
@{
ViewBag.Title = "Edit";
}
@using (Html.BeginForm())
{
<div id="productarea">
<div id="tabs-1">
@foreach (var pd in Model.Products)
{
<div>
<div style=" border: 1px solid black; margin-top:5px; padding-left: 10px; padding-bottom: 10px;" class="media p-t20px m-tn
">
<div class="left m-r10px">
<i class="fa fa-envelope-o comments-icon cmtIcon"></i>
</div>
<div class="media-body">
<p>
<span class="cmtText"> | @pd.Name | @pd.UnitPrice </span>
</p>
</div>
</div>
</div>
}
</div>
</div>
您的订单
<img src="~/Content/themes/base/images/locationicon.png" />
@for(int i = 0; i < Model.Orders.Count; i++) { @Html.EnumDropDownListFor(m => m.Orders[i].Area, new { @name = "area", @style = "width:295px; height:25px;margin-left:5px;" })
@Html.ValidationMessageFor(m => m.Orders[i].Area)}
<br /><br /><br />
<img style="margin-left:20px;" src="~/Content/themes/base/images/timeicon.png" title="Delivery Time" /> 1 hr <img style="margin-left:60px;" src="~/Content/themes/base/images/deliveryicon.png" title="Delivery Free" /> Free<img style="margin-left:60px;" src="~/Content/themes/base/images/walleticon.png" title="Minimum Order" /> Rs.1000
<br /><br />
<div style="height:150px;background-color:#fff;margin-left:-11px;margin-right:1px;">
<br /><br /><br />
<label style="float:right;color:red;margin-right:10px;">Start by Adding Items to Order!</label>
</div>
@for (int i = 0; i < Model.Orders.Count; i++) {
@Html.LabelFor(m =>m.Orders[i].SubTotal)
@Html.TextBoxFor(m => m.Orders[i].SubTotal, new {@readonly = "readonly" ,@style = "width:100px; float:right;margin-top:-21px;" })
<br />
@Html.LabelFor(m => m.Orders[i].Discount)
@Html.TextBoxFor(m => m.Orders[i].Discount, new { @readonly = "readonly", @style = "width:100px; float:right;margin-top:-21px;" })
<br />
@Html.LabelFor(m => m.Orders[i].DeliveryFee)
@Html.TextBoxFor(m => m.Orders[i].DeliveryFee, new { @readonly = "readonly", @style = "width:100px; float:right;margin-top:-21px;" })
<br />
@Html.LabelFor(m => m.Orders[i].TotalAmount)
@Html.TextBoxFor(m => m.Orders[i].TotalAmount, new { @readonly = "readonly", @style = "width:100px; float:right;margin-top:-21px;" })
}<br /><br />
<input id="btnproceed" type="button" value="Proceed to Order" />
</div>
<div id="customerdetails">
<label>Your Information</label><br />
@for (int i = 0; i < Model.Orders.Count; i++) {
@Html.TextBoxFor(m => m.Orders[i].Customer, new { @placeholder = "Enter Your Full Name" })
@Html.ValidationMessageFor(m => m.Orders[i].Customer)
@Html.TextBoxFor(m => m.Orders[i].Phone, new { @placeholder = "Enter Your Cell Number" })
@Html.ValidationMessageFor(m => m.Orders[i].Phone)
@Html.TextAreaFor(m => m.Orders[i].Address, new {@rows = 3, @cols = 2, @style = "width:300px;" , @placeholder = "Enter Your Complete Delivery Address" })
@Html.ValidationMessageFor(m => m.Orders[i].Address)
}
<input id="btnback" type="button" value="Back" /> <input id="submit" type="submit" value="Save" />
</div>
}
【问题讨论】:
-
您不能使用
tuple绑定到表单控件。使用视图模型。 -
@StephenMuecke 我需要两个视图模型,那么我该怎么办?
-
您不需要 2 个视图模型 - 您需要一个具有 2 个属性的视图模型。
-
我已经这样做了,但是在 foreach 行上出现错误不包含“产品”的定义并且没有扩展方法“产品”
-
另一个不言自明的错误 - 您的 GET 方法没有将
ProductOrderViewModel的实例传递给视图 - 您将List<ProductViewModel>传递给视图,因此出现错误。修复你的控制器代码。
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor