模型绑定到列表

发布于 2024-11-19 08:59:43 字数 2306 浏览 6 评论 0原文

如何制作默认模型绑定句柄列表?

假设我有一个 ShoppingCart 类,其中有一个 ShoppingCartItem 列表:

Public Class ShoppingCart
  Public Property CouponCode As String
  Public Property Items As New List(Of ShoppingCartItem)
End Class

Public Class ShoppingCartItem
  Public Property Title As String
  Public Property Count As Integer
End Class

我的视图如下所示(根据 这个,和,博客文章):

@ModelType VAVTag.LuckyDraw.ShoppingCart

@Code
  Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head runat="server">
  <title>Index</title>
</head>
<body>
  @Using Html.BeginForm()
    @<fieldset>
      <legend>ShoppingCart</legend>
      @Html.TextBoxFor(Function(m) m.CouponCode)
      <hr />
      @Html.EditorFor(Function(m) m.Items)

      <p>
        <input type="submit" value="Save" />
      </p>
    </fieldset>
  End Using
</body>
</html>

这些项目是使用 EditorTemplate 渲染(感谢 Darin):

@ModelType ShoppingCartItem
@Html.EditorFor(Function(m) m.Title )
@Html.EditorFor(Function(m) m.Count )
<br />

在我的控制器中,我插入一些随机数据,该视图很好地呈现了我的购物车,其中包含三件商品。

Public Class ShoppingCartController
  Inherits System.Web.Mvc.Controller

  Function Index() As ActionResult
    Dim model As New ShoppingCart
    model.Items.Add(New ShoppingCartItem With {.Count = 1, .Title = "Item A"})
    model.Items.Add(New ShoppingCartItem With {.Count = 17, .Title = "Item B"})
    model.Items.Add(New ShoppingCartItem With {.Count = 100, .Title = "Item C"})

    Return View(model)
  End Function

  <HttpPost()>
  Function Index(model As ShoppingCart) As ActionResult
    Return View(model) ' model is empty!
  End Function
End Class

但是,当我提交页面时,没有拾取任何值,甚至没有拾取优惠券代码字段。模型对象为空。什么给?

最终,我的目标是通过 JavaScript 在客户端添加/删除项目,然后让 modelbinder 在提交页面时自动获取更改。

更新:我只是在模型属性声明中缺少 Property 关键字。已经太晚了,我得睡一会儿了。 :)

How do I make the default modelbinding handle Lists?

Let’s say I have a ShoppingCart class, which has a ShoppingCartItem List:

Public Class ShoppingCart
  Public Property CouponCode As String
  Public Property Items As New List(Of ShoppingCartItem)
End Class

Public Class ShoppingCartItem
  Public Property Title As String
  Public Property Count As Integer
End Class

My view looks like this (as per the advice from this, and this, blogpost):

@ModelType VAVTag.LuckyDraw.ShoppingCart

@Code
  Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head runat="server">
  <title>Index</title>
</head>
<body>
  @Using Html.BeginForm()
    @<fieldset>
      <legend>ShoppingCart</legend>
      @Html.TextBoxFor(Function(m) m.CouponCode)
      <hr />
      @Html.EditorFor(Function(m) m.Items)

      <p>
        <input type="submit" value="Save" />
      </p>
    </fieldset>
  End Using
</body>
</html>

The items are rendered using an EditorTemplate (thanks Darin):

@ModelType ShoppingCartItem
@Html.EditorFor(Function(m) m.Title )
@Html.EditorFor(Function(m) m.Count )
<br />

In my controller, I’m inserting some random data, and the view renders my shoppingcart nicely, with three items.

Public Class ShoppingCartController
  Inherits System.Web.Mvc.Controller

  Function Index() As ActionResult
    Dim model As New ShoppingCart
    model.Items.Add(New ShoppingCartItem With {.Count = 1, .Title = "Item A"})
    model.Items.Add(New ShoppingCartItem With {.Count = 17, .Title = "Item B"})
    model.Items.Add(New ShoppingCartItem With {.Count = 100, .Title = "Item C"})

    Return View(model)
  End Function

  <HttpPost()>
  Function Index(model As ShoppingCart) As ActionResult
    Return View(model) ' model is empty!
  End Function
End Class

However, when I submit the page, no values are picked up, not even the CouponCode field. The model object is empty. What gives?

Ultimately, my goal is to add/remove items clientside via javascript and then have the modelbinder pick up the changes automatically when the page is submitted.

Update: I was just missing the Property keyword from my model properties declarations. It's too late, and I got to get some sleep. :)

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

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

发布评论

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

评论(1

や三分注定 2024-11-26 08:59:43

如何制作默认模型绑定句柄列表?

默认模型绑定器已经可以很好地处理列表和字典

只需使用编辑器模板即可。例如:

视图模型:

public class ShoppingCartItem
{
    public string Title { get; set; }
    public int Count { get; set; }
}

public class ShoppingCart
{
    public string CouponCode { get; set; }
    public List<ShoppingCartItem> Items { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new ShoppingCart
        {
            Items = new[]
            {
                new ShoppingCartItem { Count = 1, Title = "Item A" },
                new ShoppingCartItem { Count = 17, Title = "Item B" },
                new ShoppingCartItem { Count = 100, Title = "Item C" },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(ShoppingCart model)
    {
        return View(model);
    }
}

视图:

@model ShoppingCart
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <fieldset>           
            <legend>ShoppingCart</legend>
            @Html.TextBoxFor(x => x.CouponCode)
            <hr/>
            @Html.EditorFor(x => x.Items)
        </fieldset>
        <p>
            <input type="submit" value="Save" />
        </p>
    }
</body>
</html>

最后是编辑器模板(~/Views/Home/EditorTemplates/ShoppingCartItem.cshtml),它将为 Items 的每个项目呈现代码>集合:

@model ShoppingCartItem
@Html.EditorFor(x => x.Title)
@Html.EditorFor(x => x.Count)

How do I make the default modelbinding handle Lists?

The default model binder already handles lists and dictionaries perfectly fine.

Just use editor templates. For example:

View model:

public class ShoppingCartItem
{
    public string Title { get; set; }
    public int Count { get; set; }
}

public class ShoppingCart
{
    public string CouponCode { get; set; }
    public List<ShoppingCartItem> Items { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new ShoppingCart
        {
            Items = new[]
            {
                new ShoppingCartItem { Count = 1, Title = "Item A" },
                new ShoppingCartItem { Count = 17, Title = "Item B" },
                new ShoppingCartItem { Count = 100, Title = "Item C" },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(ShoppingCart model)
    {
        return View(model);
    }
}

View:

@model ShoppingCart
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <fieldset>           
            <legend>ShoppingCart</legend>
            @Html.TextBoxFor(x => x.CouponCode)
            <hr/>
            @Html.EditorFor(x => x.Items)
        </fieldset>
        <p>
            <input type="submit" value="Save" />
        </p>
    }
</body>
</html>

and finally the editor template (~/Views/Home/EditorTemplates/ShoppingCartItem.cshtml) which will be rendered for each item of the Items collection:

@model ShoppingCartItem
@Html.EditorFor(x => x.Title)
@Html.EditorFor(x => x.Count)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文