【问题标题】:Non sequential list binding not working非顺序列表绑定不起作用
【发布时间】:2011-10-18 12:22:37
【问题描述】:

根据article,我正在尝试绑定非连续项目的列表。

查看:

<%using (Html.BeginForm("Products", "Home", FormMethod.Post))
{ %>
    <input type="hidden" name="products.Index" value="cold" />
    <input type="text" name="products[cold].Name" value="Beer" />
    <input type="text" name="products[cold].Price" value="7.32" />
    <input type="hidden" name="products.Index" value="123" />
    <input type="text" name="products[123].Name" value="Chips" />
    <input type="text" name="products[123].Price" value="2.23" />
    <input type="hidden" name="products.Index" value="caliente" />
    <input type="text" name="products[caliente].Name" value="Salsa" />
    <input type="text" name="products[caliente].Price" value="1.23" />
    <input type="submit" value="Submit" />
<%} %>

动作方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Products(IList<Product> products)
{
    return View("Index");
}

绑定似乎对我不起作用,参数 products 总是包含 null。我错过了什么吗?

非常感谢任何帮助,谢谢。

请注意,我使用的是 ASP.NET MVC 1.0

【问题讨论】:

  • 您在这里拼错了产品:&lt;%using (Html.BeginForm("Producs", "Home", FormMethod.Post))。您只是在这里输入了错误的代码,还是您的实际代码也是这样?编辑:没关系。看看达林发布了什么。
  • 感谢您发现这一点。这是一个错字,我现在就编辑它。

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


【解决方案1】:

从 ASP.NET MVC 2.0 开始,默认模型绑定器能够绑定具有非顺序索引的集合。这在 ASP.NET MVC 1.0 中不受支持。

【讨论】:

  • 好的,谢谢达林。有没有其他方法可以在 MVC 1.0 中实现这一点?
  • @User123343,除了编写自定义模型绑定器之外,我看不到太多可能性。事实上,升级到更新的版本将是最好的解决方案恕我直言。随着即将发布的 ASP.NET 4.0,您将落后 4 代,并积累越来越多的缺失功能。
  • 好的,非常感谢达林的帮助。目前正在升级,所以我们很快就会切换到 .net 4.0 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多