ASP.NET 绑定到 IEnumerable

发布于 2024-08-08 01:58:30 字数 1660 浏览 2 评论 0原文

我将 IEnumerable 类型传递给我的视图,对于每个项目,我输出一个 html.textbox 以输入详细信息。

当我将其发送回我的控制器时,集合是空的,我不明白为什么。

    public class Item
    {
        public Order Order { get; set; }
        public string Title { get; set; }
        public double Price { get; set; }
    }

我的获取方法:

public ActionResult AddItems(Order order)
    {
        Item itemOne = new Item
        {
            Order = order
        };

        Item itemTwo = new Item
        {
            Order = order,
        };

        IList<Item> items = new List<Item> { itemOne, itemTwo };

        return View(items);
    }

视图:

            <% int i = 0; foreach (var item in Model)
           { %>

            <p>
                <label for="Title">Item Title:</label>
                <%= Html.TextBox("items[" + i + "].Title") %>
                <%= Html.ValidationMessage("items[" + i + "].Title", "*")%>
            </p>
            <p>
                <label for="Price">Item Price:</label>
                <%= Html.TextBox("items[" + i + "].Price") %>
                <%= Html.ValidationMessage("items[" + i + "].Price", "*")%>
            </p>

        <% i++;
           } %>

POST 方法:

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddItems(IEnumerable<Item> items)
    {
        try
        {

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

目前我只是在 post 方法上有一个断点来检查我要返回的内容。

I'm passing a the type IEnumerable to my view, and for each item I output a html.textbox to enter the details into.

When I post this back to my controller, the collection is empty and I can't see why.

    public class Item
    {
        public Order Order { get; set; }
        public string Title { get; set; }
        public double Price { get; set; }
    }

My Get method:

public ActionResult AddItems(Order order)
    {
        Item itemOne = new Item
        {
            Order = order
        };

        Item itemTwo = new Item
        {
            Order = order,
        };

        IList<Item> items = new List<Item> { itemOne, itemTwo };

        return View(items);
    }

The View:

            <% int i = 0; foreach (var item in Model)
           { %>

            <p>
                <label for="Title">Item Title:</label>
                <%= Html.TextBox("items[" + i + "].Title") %>
                <%= Html.ValidationMessage("items[" + i + "].Title", "*")%>
            </p>
            <p>
                <label for="Price">Item Price:</label>
                <%= Html.TextBox("items[" + i + "].Price") %>
                <%= Html.ValidationMessage("items[" + i + "].Price", "*")%>
            </p>

        <% i++;
           } %>

The POST method:

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddItems(IEnumerable<Item> items)
    {
        try
        {

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

At the moment i just have a breakpoint on the post method to check what i'm gettin back.

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

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

发布评论

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

评论(3

蓝天 2024-08-15 01:58:30

尝试添加以下内容:

<input type="hidden" name="items.Index" value="<%=i%>" />

进一步...

我擅自更改了 for 循环的方式,以便您不再需要在代码中进行递增,就像我一样总觉得这有点复杂。这意味着您将调用当前项目为:

<%= item.Current %>

并且您可以使用以下方式访问循环的当前索引:

<%= item.Index %>

因此您的视图将如下所示(尽管您似乎从未使用当前项目,只是使用它的索引):

<% foreach(var item in Model.Select((x, i) => new { Current = x, Index = i }) { %>
  <p>
    <label for="Title">Item Title:</label>
    <input type="hidden" name="items.Index" value="<%= item.Index %>" />
    <%= Html.TextBox("items[" + item.Index + "].Title") %>
    <%= Html.ValidationMessage("items[" + item.Index + "].Title", "*")%>
  </p>
  <p>
    <label for="Price">Item Price:</label>
    <%= Html.TextBox("items[" + item.Index + "].Price") %>
    <%= Html.ValidationMessage("items[" + item.Index + "].Price", "*")%>
  </p>
<% } %>

Try adding this:

<input type="hidden" name="items.Index" value="<%=i%>" />

Further...

I took the liberty of changing how you do your for loop so that you no longer need to do the incrementing in the code, as I always find this a bit convoluted. This means that you would call the current item as:

<%= item.Current %>

And you access the current index of the loop with:

<%= item.Index %>

So your view would look like this (although you never seem to use the current item anyway, just the index of it):

<% foreach(var item in Model.Select((x, i) => new { Current = x, Index = i }) { %>
  <p>
    <label for="Title">Item Title:</label>
    <input type="hidden" name="items.Index" value="<%= item.Index %>" />
    <%= Html.TextBox("items[" + item.Index + "].Title") %>
    <%= Html.ValidationMessage("items[" + item.Index + "].Title", "*")%>
  </p>
  <p>
    <label for="Price">Item Price:</label>
    <%= Html.TextBox("items[" + item.Index + "].Price") %>
    <%= Html.ValidationMessage("items[" + item.Index + "].Price", "*")%>
  </p>
<% } %>
谁许谁一生繁华 2024-08-15 01:58:30

这不起作用的原因是因为默认模型绑定器不知道您想要将每个表单元素放入 IEnumerable 中。

您必须使 AddItems Post 方法中的模型与默认模型绑定器的表单元素相匹配,才能正确放入数据。

或者,您可以直接通过表单变量来处理它。

The reason this isn't working is because the default model binder has no clue you want to put each of your form elements into an IEnumerable.

You have to make your model in your AddItems Post method match the form elements for the default model binder to put the data in properly.

Or, you can just handle it directly through Form variables.

一城柳絮吹成雪 2024-08-15 01:58:30

我想知道复杂类型的数组是否可以在没有活页夹的情况下工作,您可以尝试制作它:

Public ActionResult AddItems(Item[] items)

I wonder if an array for complex types works with out a binder you could try making it:

Public ActionResult AddItems(Item[] items)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文