创建视图正在发布空对象

时间:2010-08-10 09:41:13

标签: 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());
    }
“%>

    创建

<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>

3 个答案:

答案 0 :(得分:12)

非常简单的解决方案:

更改您的操作签名
public ActionResult Create(UserExtended user)

public ActionResult Create(UserExtended UserExtended)

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

希望这有帮助!

答案 1 :(得分:3)

我遇到了一个非常类似的问题,但在我的案例中发现我必须匹配数据库表名而不是类型名称

  • 类型名称:NonTradingDay
  • 数据库表名称:dbo.NonTradingDays(已多元化

创建方法:

[HttpPost]
public ActionResult Create(NonTradingDay NonTradingDays)
{
    if (ModelState.IsValid)
    {
        db.NonTradingDay.Add(NonTradingDays);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }
    return View(NonTradingDays);
}

我曾尝试'NonTradingDay',但仍然无效;然后我查看了数据库表名,尝试了'NonTradingDays'并进行了映射(参数不再为null)。

认为这是因为我有数据库上下文:

public DbSet<NonTradingDay> NonTradingDay { get; set; }

而不是:

public DbSet<NonTradingDay> NonTradingDays { get; set; }

答案 2 :(得分:0)

您正在返回UserExtended类的新实例

返回查看(新的UserExtended());

而是返回您获得的对象作为参数

返回用户