2010-08-10 77 views
4

應該是一個容易回答的問題。 我想在視圖中創建一個對象。包含該對象的類由一個User類和一個密碼組成。 當我點擊提交按鈕時,Controller會爲密碼和用戶選取空值。 查看下面的容器類,控制器和視圖;創建視圖張貼空對象

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> 

回答

12

非常簡單的解決方案的目標:從

public ActionResult Create(UserExtended user) 

更改您的行動簽名

public ActionResult Create(UserExtended UserExtended) 

這樣ModelBinder就會知道如何從Request中重新組裝對象。

希望這會有所幫助!

+1

太棒了!我認爲答案很簡單,但這是學習MVC,恕我直言,你必須犯的錯誤之一。 – arame3333 2010-08-10 10:20:00

+1

所以真的:)很高興我能幫忙! – 2010-08-10 10:24:44

+0

過去3天我也有同樣的問題!簡直不敢相信!哈哈!謝謝! – BrunoSalvino 2010-10-07 20:10:57

0

要返回UserExtended類的新實例

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

,而不是回到你作爲參數

回報用戶

+0

這並不真的有什麼關係的事實,比如他得到的行動是空的... – 2010-08-10 10:13:01

+0

Yngvebn是正確的 – arame3333 2010-08-10 10:18:49

+0

@ arame3333你嘗試只是返回用戶var – 2010-08-10 10:26:50

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'並且進行了映射(參數不再爲空)。

認爲這是因爲我有數據庫上下文爲:

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

不是:

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