【问题标题】:MVC Model Binding not binding simple object on nested loopsMVC模型绑定不绑定嵌套循环上的简单对象
【发布时间】:2012-12-26 23:05:23
【问题描述】:

具有这种结构:

Index.cshtml:

@foreach (var category in Model) {
    @Html.DisplayFor(m => category.Products)
}

Products.cshtml:

...
@Html.Partial("_AddToCartProductViewModel", new CheckoutVC.Models.ModelView.AddToCartProductViewModel(Model))
...

_AddToCartProductViewModel.cshtml:

@model CheckoutVC.Models.ModelView.AddToCartProductViewModel
@using (Ajax.BeginForm("AddToCart", "Cart", new AjaxOptions { LoadingElementId = "loading" + Model.IdProduct, OnSuccess = "showMessage", UpdateTargetId = "cart_widget" })) {
    @Html.HiddenFor(m => m.IdProduct)
    @Html.HiddenFor(m => m.IdPrescriptionType)
    @Html.HiddenFor(m => m.PrescriptionRequired)
    @Html.HiddenFor(m => m.Quantity)
    <span class="p-r-s">@Html.LabelFor(m=>m.IdPrescriptionType)</span>
    @Html.DropDownListFor(m=>m.IdPrescriptionType, new SelectList(Model.PrescriptionOptions, "Item1", "Item2"), String.Empty) 
    <button class="action add @Html.PrescriptionRequiredCss(Model.PrescriptionRequired)" type="submit">agregar al carrito<img class="loading" id="loading@(Model.IdProduct)" alt="" src="@Url.Content("~/Content/images/shared/loading_16x16.gif")" width="16" height="16" /></button>
}

有了这个 AddToCartProductViewModel.cs 构造函数:

public AddToCartProductViewModel(ProductCheckoutMinVO product, int quantity = 1) {
    IdProduct = product.Id;
    PrescriptionRequired = product.PrescriptionRequired;
    Quantity = 1;
    Promotions = product.Promotions;
}

[Required]
[Min(1)]
public int IdProduct { get; set; }

public int Quantity { get; set; }

public bool? PrescriptionRequired { get; set; }

[Min(0)]
public int IdPrescriptionType { get; set; }

MVC 在提交时生成此请求:

category.Products[0].IdProduct:826
category.Products[0].IdPrescriptionType:0
category.Products[0].PrescriptionRequired:False
category.Products[0].Quantity:1
category.Products[0].IdPrescriptionType:1
X-Requested-With:XMLHttpRequest

问题是,我的控制器 CartController.cs :

public RedirectToRouteResult AddToCart(AddToCartProductViewModel product, FormCollection form, string returnUrl = null) {
...
}

FormCollection(表单)接收参数,而 AddToCartProductViewModel(产品)不绑定。

我有一些想法,为什么属性没有绑定到产品,以及我如何在这里和那里做一些魔术来从一些嵌套循环中填充一个单一对象表单(其中一个期望请求中的集合对象 [一个是framework]),但找不到一个优雅的解决方案来将这种表单场景绑定到 AddToCartProductViewModel。

我可以在 AddToCart 方法中直接使用属性使其“以某种方式”工作,但随后我失去了模型视图上的验证(数据注释)。

如何让 MVC 将这些属性绑定到 AddToCartProductViewModel 视图模型

【问题讨论】:

  • 你能展示完整的 AddToCartProductViewModel 吗?尤其是公共财产?
  • 完成,我有 {get; set;},这不是问题。

标签: asp.net-mvc binding model-binding


【解决方案1】:

我觉得你可以试试:

public RedirectToRouteResult AddToCart([Bind(Prefix="category.Products[0]")]AddToCartProductViewModel product, FormCollectionform, string returnUrl = null) {
    ...
}

【讨论】:

    【解决方案2】:

    解决一些问题会带来另一个问题。 最终使用 html 标签手动完成,并保留数据注释验证。

    可能是一个混乱的视图模型问题,我只是不知道,也没有时间或资源让它像我现在想要的那样工作。也许我会在未来尝试解决。

    不知道是否有办法结束问题或该怎么做?编辑可以随意更新。 :D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      相关资源
      最近更新 更多