【问题标题】:How to bind View with relevant model when two models(Tuple) are there当有两个模型(元组)时如何将视图与相关模型绑定
【发布时间】: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&lt;ProductViewModel&gt; 传递给视图,因此出现错误。修复你的控制器代码。

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

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

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)

【讨论】:

  • @foreach 行出现错误不包含“产品”的定义,也没有扩展方法“产品”
  • @AhmadCheema 应该是 Model.Products 而不是 Model.Product 介意。
  • 亲爱的我已经在模型中写了 Product 所以不能在这里写额外的 s ......对吗?
  • 亲爱的,现在我在第 72 行遇到错误:@Html.EnumDropDownListFor(m => m.区域,新错误:Cygnus.Global.ViewModels.ProductOrderViewModel' 不包含“区域”的定义和
  • @AhmadCheema 把public List&lt;ProductViewModel&gt; product { get; set; }改成public List&lt;ProductViewModel&gt; Products { get; set; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多