我正在构建一个 ASP.NET MVC 2 应用程序并有一个包含以下操作的控制器:
public ActionResult Edit()
{
...
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(EditUser user)
{
...
}
为此,我得到了一个如下所示的强类型视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPages/DefaultMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MyApp.Views.ViewClasses.EditUser>" %>
<%@ Import Namespace="MyApp.Views.Helpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Edit", "Account", FormMethod.Post, new { enctype = "multipart/form-data", @id = "edit_account" }))
{%>
<%: Html.LabelFor(model => model.User.UserEmail, false) %>
<%: Html.TextBoxFor(model => model.User.UserEmail, new { @class = "tb1" })%>
...
<% } %>
</asp:Content>
当点击提交按钮(代码中未显示)时,将点击公共 ActionResult Edit(EditUser user) 操作,但用户对象将不包含任何数据?
这是 html 的一部分的样子:
<div class="controlTitleContainer"><div class="text"><label for="User_UserEmail">Mailadress</label></div></div>
<input type="text" value="" name="User.UserEmail" id="User_UserEmail" class="tb1">
这应该意味着输入指向正确的属性。
值得一提的是,我使用 Data annotation 来验证发送到 action 的对象,但即使有几个 requre 字段,模型也始终有效。
知道为什么会这样吗?