ASP.NET MVC模型绑定一个IList<>参数 [英] ASP.NET MVC model binding an IList<> parameter

查看:214
本文介绍了ASP.NET MVC模型绑定一个IList<>参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[我解决了这个自己,看我为事业的答案]

我有一个IList的和麻烦的表单值LT;>参数设置中的一个正确控制器的方法

I'm having trouble getting form values for a IList<> argument in a controller method set properly.

我的控制器类看起来是这样的:

My controller class looks like this:

public class ShoppingBasketController : Controller {

    public ActionResult Index() {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Add(IList<ShoppingBasketItem> items) {
        Session["basket"] = items; // for testing
        return RedirectToAction("Index");
    }
}
public class ShoppingBasketItem {
     public int ItemID;
     public int ItemQuantity;
}

略带修剪形式:

<% using (Html.BeginForm("Add", "ShoppingBasket")) { %>
    <% int codeIndex = 0;
    foreach (Product product in products) { %>
        <%= Html.Hidden("items[" + codeIndex + "].ItemID", product.Id) %>
        <%= Html.TextBox("items[" + codeIndex + "].ItemQuantity", "0", new { size = "2"}) %>
        <% codeIndex++;
    }
} %>

这会产生类似的标记:

Which produces markup like:

<form action="/Basket/Add" method="post">
    <input id="items[0]_ItemID" name="items[0].ItemID" type="hidden" value="1" />
    <input id="items[0]_ItemQuantity" name="items[0].ItemQuantity" size="2" type="text" value="0" />

    <input id="items[1]_ItemID" name="items[1].ItemID" type="hidden" value="2" />
    <input id="items[1]_ItemQuantity" name="items[2].ItemQuantity" size="2" type="text" value="0" />

    <input id="items[2]_ItemID" name="items[2].ItemID" type="hidden" value="3" />
    <input id="items[2]_ItemQuantity" name="items[2].ItemQuantity" size="2" type="text" value="0" />
</form>

我已经签了获得提交表单的价值观和他们是正确的。正确的号码 ShoppingBasketItem S还得到付诸会话[菜篮子],但无论是<$ C $的C>项目编号和 ItemQuantity 为零。这似乎是正确解码格式值的列表,而不是拿起属性本身。

I've checked the form values that get submitted and they are correct. The correct number of ShoppingBasketItems also get put into Session["basket"], however both the ItemID and ItemQuantity of each are zero. It appears to be correctly decoding the list of form values, but not picking up the properties themselves.

我使用MVC RC2林,和基于<一href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\">article由Scott Hanselman在我pretty相信我的code是正确的。我缺少的东西吗?

I'm using MVC RC2, and based on an article by Scott Hanselman I'm pretty sure my code is correct. Am I missing something?

推荐答案

解决方案

在下载MVC源码我还是不明白为什么它不会工作后,让我presumed它必须是与我试图绑定类型。果然,作为成员变量的值,而不是性能,是罪魁祸首。这是因为模型绑定使用反射来设置属性,它不是通过电话寻求 TypeDescriptor.GetProperties(类型)

After downloading the MVC source I still couldn't see why it wouldn't work, so I presumed it must be something to do with the type I was attempting to bind. Sure enough, the values being member variables, as opposed to properties, was the culprit. This is because the model binder uses reflection to set properties, which it wasn't finding through the call to TypeDescriptor.GetProperties(Type).

更新值类这一解决它(击中头部防渗墙我要补充!!小时后):

Updating the value class to this solved it (after hours of hitting head off wall I should add!!):

public class ShoppingBasketItem {
    public int ItemID { get; set; }
    public int ItemQuantity { get; set; }
}

这篇关于ASP.NET MVC模型绑定一个IList&LT;&GT;参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆