C# 当存在两个模型(元组)时,如何将视图与相关模型绑定
我在MVC 4中工作,希望在视图中绑定两个使用Tuple的模型,现在我希望从两个不同div中的两个模型中获取值,但我得到以下错误:C# 当存在两个模型(元组)时,如何将视图与相关模型绑定,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我在MVC 4中工作,希望在视图中绑定两个使用Tuple的模型,现在我希望从两个不同div中的两个模型中获取值,但我得到以下错误: here is my Controller code: public ActionResult Edit() { CygnusInternalResponseViewModel result = new CygnusInternalResponseViewModel(); result
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 FooViewModel
{
public List<ProductViewModel> Products {get;set;}
public OrderViewModel Order {get; set;}
}
还请注意,如果其余字段与订单相关,则它将位于订单
属性中,即:
@Html.LabelFor(m =>m.Order.SubTotal)
不能使用
元组来绑定表单控件。使用视图模型。@StephenMuecke我需要两个视图模型,那么我该怎么办?您不需要两个视图模型-您需要一个具有两个属性的视图模型。我已经这样做了,但foreach行上的getting error不包含“Product”的定义,并且没有扩展方法“Product”另一个自解释的错误-您的GET方法没有通过ProductOrderViewModel
的实例传递给视图-您将列表传递给视图,因此出现错误。修复您的控制器代码。@foreach line上的获取错误不包含“Product”的定义,也不包含扩展方法“Product”@AhmadCheema它应该是Model。Products
不是Model。Product
mind。亲爱的,我已经在Model中编写了产品,所以不能在这里使用额外的s来编写…对吗?亲爱的,现在我在第行中遇到了错误72:@Html.EnumDropDownListFor(m=>m.Area,新错误:Cygnus.Global.ViewModels.ProductOrderViewModel'不包含“Area”的定义and@AhmadCheema只需将其更改为公共列表产品{get;set;}
到公共列表产品{get;set;}
<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>
}
public class FooViewModel
{
public List<ProductViewModel> Products {get;set;}
public OrderViewModel Order {get; set;}
}
@model FooViewModel
// code ommited
@foreach (var pd in Model.Products)
{
// rest of code
@Html.LabelFor(m =>m.Order.SubTotal)