POST 操作方法中的强类型 ViewModel 仅包含 null 值

2024-01-08

我正在尝试使用强类型视图来实现我的编辑操作方法,该视图接收自定义形状的 ViewModel 类。换句话说,我想要一个强类型的 ViewModel,其中包含应编辑的 Linq 实体以及应在视图中显示的一些其他对象。

我可以在调用 GET Edit 操作方法时看到视图,但强类型 POST 操作方法仅接收带有 null 参数的 ViewModel 类,并且我不知道如何检索 POST 参数。

视图模型如下所示:

//my custom-shaped ViewModel
public class CustomersFormViewModel
{
    public SelectList AccountTypesDropDownBox;
    public SelectList CountriesDropDownBox;
    public Customer Customer;
}

操作方法如下所示:

//
// GET: /CustomersController/Edit   

public ActionResult Edit(int ID)
{
   var model = new CustomersFormViewModel
                    {
                        Customer = repository.Load(ID.Value),
                        CountriesDropDownBox = GetCountries(),
                        AccountTypesDropDownBox = GetAccountTypes()
                    };
    return View(model);
}

//
// POST: /CustomersController/Edit  

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomersFormViewModel model)
{
    //THE NEXT LINE THROWS!!!
    Debug.Assert(Model.Customer!=null);
    return View(model);
}

这是我的编辑视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/CustAdminMaster.master"
    Inherits="System.Web.Mvc.ViewPage<Zeiterfassung.Controllers.CustomersController+CustomersFormViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    NewEdit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        NewEdit</h2>
    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
    <% using (Html.BeginForm())
       {%>
    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="FirstName">FirstName:</label>
            <%= Html.TextBox("FirstName",Model.Customer.FirstName) %>
            <%= Html.ValidationMessage("FirstName", "*") %>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
    <% } %>
    <div>
        <%=Html.ActionLink("Back to List", "Index") %>
    </div>
</asp:Content>

我还尝试了带有 formValues 参数的 POST 操作方法,但视图模型仍然不包含发布的参数:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int ID, FormCollection formValues)
{
      CustomersFormViewModel model = new CustomersFormViewModel();            
      UpdateModel(model);
      //THE NEXT LINE STILL THROWS
      Debug.Assert(model.Customer!=null);
      return View("NewEdit",model);
}

到目前为止,我发现的唯一方法是编写自己的代码,从 FormCollection 中获取发布的参数并相应地更新我的自定义 ViewModel。但这种方法似乎有点原始。难道真的没有更好的办法吗?

EDIT:我刚刚在视图中尝试了 tvanfosson 建议的不同语法,但问题仍然没有改变:

<label for="Customer.FirstName">FirstName:</label>
    <%= Html.TextBox("Customer.FirstName") %>
    <%= Html.ValidationMessage("Customer.FirstName", "*") %>

您需要根据模型中的前缀来命名字段。此外,您将需要修改视图模型以使用属性而不是字段。默认模型绑定器在绑定时仅查看模型上的公共属性。

    <label for="Customer.FirstName">FirstName:</label>
    <%= Html.TextBox("Customer.FirstName") %>
    <%= Html.ValidationMessage("Customer.FirstName", "*") %>

这样,模型绑定器就知道如何将表单参数与模型的适当组件和关联属性关联起来。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POST 操作方法中的强类型 ViewModel 仅包含 null 值 的相关文章

随机推荐