绑定 IList<> 的 ASP.NET MVC 模型 范围

发布于 2024-07-15 02:55:51 字数 2067 浏览 6 评论 0 原文

[我自己解决了这个问题,请参阅我的答案以了解原因]

我在获取 IList<> 的表单值时遇到问题。 控制器方法中的参数设置正确。

我的控制器类如下所示:

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++;
    }
} %>

生成的标记如下:

<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 也会放入 Session["basket"] 中,但是 ItemIDItemQuantity > 每个都为零。 它似乎正确解码了表单值列表,但没有获取属性本身。

我正在使用 MVC RC2,并且基于 Scott Hanselman 的文章 我很漂亮确保我的代码是正确的。 我错过了什么吗?

[I solved this myself, see my answer for cause]

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

The slightly trimmed form:

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-07-22 02:55:51

解决方案

下载 MVC 源代码后,我仍然不明白为什么它不起作用,所以我推测它一定与我尝试绑定的类型有关。 果然,罪魁祸首是作为成员变量的值,而不是属性。 这是因为模型绑定程序使用反射来设置属性,而它没有通过调用 TypeDescriptor.GetProperties(Type) 找到这些属性。

将值类更新为此解决了这个问题(经过几个小时的努力,我应该添加!!):

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

Solution

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文