当两个模型(元组)存在时,如何将视图与相关模型绑定

本文关键字:模型 视图 绑定 两个 元组 存在 | 更新日期: 2023-09-27 18:08:41

我在MVC 4中工作,并希望绑定视图中的两个模型,我使用元组,现在我想从两个不同的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>

}

当两个模型(元组)存在时,如何将视图与相关模型绑定

创建一个包含两个模型的类,并从控制器填充它们:

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

还要注意,如果其余字段与顺序相关,则将在Order属性内,即:

@Html.LabelFor(m =>m.Order.SubTotal)