更改为使用 ViewModel 后 MVC3 ModelState 失败

发布于 2024-10-30 19:27:14 字数 1424 浏览 0 评论 0原文

我已更改控制器以传递 VoucherBatchViewModel 而不是 VoucherBatch

 [HttpPost]
    public ActionResult Edit(Guid id, VoucherBatchViewModel voucherBatchViewModel)
    {
        if (!ModelState.IsValid)
        {
            SetupDropDowns();

并且虚拟机:

 public class VoucherBatchViewModel
{
    public VoucherBatch VoucherBatchInVM { get; set; }
    public string CreationReference { get; set; }
    public int NumberToMove { get; set; }
    public int VoucherCodeLength { get; set; }
}

问题: 为什么我的 ModelState 无效?下拉列表未填充 ViewModel 中的正确字段。 html 确实生成了正确的内容

<div class="editor-field">
    @Html.DropDownList("VoucherProviderId",
            new SelectList(ViewBag.VoucherProviders as System.Collections.IEnumerable,
            "Id", "Name", Model.VoucherBatchInVM.VoucherProviderId))

,并且 Edit get 包含了我用来填充 DropDown 的内容。

 ViewBag.VoucherProviders = uow.VoucherProviders.OrderBy(v => v.Name).ToList();

网页:

    <select id="VoucherProviderId" name="VoucherProviderId"><option value="0469f9ba-c4ea-401a-86f1-095208c6a7fb">Name</option>
<option selected="selected" value="e0aeed44-3574-46f1-a493-0a6a87948942">Voucher Provider 1</option>
<option value="5abe1158-282b-4330-9b11-01de503a2f16">Voucher Provider 2</option>

I've changed my controller to pass a VoucherBatchViewModel instead of a VoucherBatch

 [HttpPost]
    public ActionResult Edit(Guid id, VoucherBatchViewModel voucherBatchViewModel)
    {
        if (!ModelState.IsValid)
        {
            SetupDropDowns();

And the vm:

 public class VoucherBatchViewModel
{
    public VoucherBatch VoucherBatchInVM { get; set; }
    public string CreationReference { get; set; }
    public int NumberToMove { get; set; }
    public int VoucherCodeLength { get; set; }
}

Question: Why is my ModelState invalid? The drop down has not populated the correct field in the ViewModel. The html does produce the correct

<div class="editor-field">
    @Html.DropDownList("VoucherProviderId",
            new SelectList(ViewBag.VoucherProviders as System.Collections.IEnumerable,
            "Id", "Name", Model.VoucherBatchInVM.VoucherProviderId))

and Edit get contains this which I use to populate the DropDown.

 ViewBag.VoucherProviders = uow.VoucherProviders.OrderBy(v => v.Name).ToList();

Html:

    <select id="VoucherProviderId" name="VoucherProviderId"><option value="0469f9ba-c4ea-401a-86f1-095208c6a7fb">Name</option>
<option selected="selected" value="e0aeed44-3574-46f1-a493-0a6a87948942">Voucher Provider 1</option>
<option value="5abe1158-282b-4330-9b11-01de503a2f16">Voucher Provider 2</option>

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

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

发布评论

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

评论(1

执妄 2024-11-06 19:27:14

在您的 VoucherBatchViewModel 中看不到任何 VoucherProviderId 属性。所以你的 HTML 无效。它应该是:

<select id="VoucherProviderId" name="VoucherBatchInVM.VoucherProviderId">
    ...
</select>

为了实现这个标记,我建议你使用强类型助手:

@Html.DropDownListFor(
    x => x.VoucherBatchInVM.VoucherProviderId,
    new SelectList(ViewBag.VoucherProviders as System.Collections.IEnumerable, "Id", "Name")
)

并且请用强类型模型属性替换这个让我呕吐的ViewBag

@Html.DropDownListFor(
    x => x.VoucherBatchInVM.VoucherProviderId,
    new SelectList(Model.VoucherProviders, "Id", "Name")
)

Can't see any VoucherProviderId property in your VoucherBatchViewModel. So your HTML is not valid. It should be:

<select id="VoucherProviderId" name="VoucherBatchInVM.VoucherProviderId">
    ...
</select>

And to achieve this markup you I would recommend you using strongly typed helpers:

@Html.DropDownListFor(
    x => x.VoucherBatchInVM.VoucherProviderId,
    new SelectList(ViewBag.VoucherProviders as System.Collections.IEnumerable, "Id", "Name")
)

And please replace this ViewBag which is making me vomit with a strongly typed model property:

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