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

时间:2021-06-25 12:16:50

I am working in MVC 4 and want to bind two models in view for which i used Tuple, and now i want to get values from both models in two different Divs but i am getting the following error:

我在MVC 4工作,并希望绑定两个模型在视图中,我使用Tuple,现在我想从两个不同的Divs中获取两个模型的值,但我收到以下错误:

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();

        }

here is my model code:

这是我的型号代码:

    public class ProductOrderViewModel
    {
        public List<ProductViewModel> Products { get; set; }

        public List<OrderViewModel> Orders { get; set; }
    }

Please guide me to tackle this issue..Following is my Code

请指导我解决这个问题。以下是我的代码

@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>

Your Order

    <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>

}

1 个解决方案

#1


2  

Create a class that contains the two models and populated them from your controller:

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

public class FooViewModel
{
 public List<ProductViewModel> Products {get;set;}
 public OrderViewModel Order {get; set;}
}

Then in your view:

然后在你看来:

@model FooViewModel
// code ommited
@foreach (var pd in Model.Products)
{
// rest of code

Also note that if the rest of the fields are related to the order it would be within the Order property i.e:

另请注意,如果其余字段与Order属性中的顺序相关,即:

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

#1


2  

Create a class that contains the two models and populated them from your controller:

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

public class FooViewModel
{
 public List<ProductViewModel> Products {get;set;}
 public OrderViewModel Order {get; set;}
}

Then in your view:

然后在你看来:

@model FooViewModel
// code ommited
@foreach (var pd in Model.Products)
{
// rest of code

Also note that if the rest of the fields are related to the order it would be within the Order property i.e:

另请注意,如果其余字段与Order属性中的顺序相关,即:

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