创建视图正在发布空对象

ara*_*333 4 asp.net-mvc

应该是一个容易回答的问题.我试图在视图中创建一个对象.包含该对象的类由User类和密码组成.当我单击提交按钮时,Controller会为Password和User选择空值.请参阅下面的Container类,Controller和View;

public class UserExtended
{
    public UserITOC User { get; set; }
    public string Password { get; set; }
}

    [Authorize]
    public ActionResult Create()
    {
        return View(new UserExtended());
    }

    //
    // POST: /Dinners/Create

    [Authorize(Roles = "Administrator")]
    [HttpPost]
    public ActionResult Create(UserExtended user)
    {
        if (ModelState.IsValid)
        {
            // Create user in the User datatable
            SqlUsersRepository sqlRepository = new SqlUsersRepository();
            ITOCEntities db = new ITOCEntities();
            db.UserITOCs.AddObject(user.User);

            // Create user as an authenticated user within the Reader role.
            int i = user.User.EmailAddress.IndexOf('@') - 1;
            string userName = user.User.EmailAddress.Substring(0, i);
            string email = user.User.EmailAddress;
            Membership.CreateUser(userName, user.Password, email);
            Roles.AddUserToRole(userName, "Reader");                // Automatically assigned as a Reader
        }
        return View(new UserExtended());
    }
Run Code Online (Sandbox Code Playgroud) "%>

创建

<h2>Create</h2>

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.User.Forename) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.User.Forename)%>
            <%: Html.ValidationMessageFor(model => model.User.Forename)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.User.Surname) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.User.Surname)%>
            <%: Html.ValidationMessageFor(model => model.User.Surname)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.User.EmailAddress) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.User.EmailAddress)%>
            <%: Html.ValidationMessageFor(model => model.User.EmailAddress)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Password) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.Password)%>
            <%: Html.ValidationMessageFor(model => model.Password) %>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>
Run Code Online (Sandbox Code Playgroud)

Yng*_*sen 12

极其简单的解决方案:

从中更改您的操作签名

public ActionResult Create(UserExtended user)
Run Code Online (Sandbox Code Playgroud)

public ActionResult Create(UserExtended UserExtended)
Run Code Online (Sandbox Code Playgroud)

这样,ModelBinder将知道如何从Request重新组装对象.

希望这可以帮助!