2014-01-28 34 views
0

我有一個去反對使用的EntityFramework一個SQL Server 2008 R2數據庫的應用程序MVC4(未編寫第一個)。mvc4控制器編輯帖子行動不充盈模型

我有一個視圖模式,即我傳遞給查看()在控制器的編輯方法。

public class UserViewModel 
{ 
    public User User { get; set; } 
    public int RoleId { get; set; } 
    public List<UserAccount> UserAccounts { get; set; }   
} 

當我嘗試保存編輯,視圖模型具有空用戶和UserAccounts和角色ID爲0

不引發錯誤 - 爲什麼它可能未正確填寫任何想法?

這裏是我的UserController.Edit行動

public ActionResult Edit(int id = 0) 
     { 
      UserViewModel vm = new UserViewModel(); 
      vm.User=_db.FindUserById(id); 
      webpages_Roles role = _db.GetRoleByUserId(id); 
      vm.RoleId = role.RoleId; 
      vm.UserAccounts = _db.GetUserAccountsByUserId(id); 
      if (vm.User == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(vm); 
     } 

[HttpPost] 
     public ActionResult Edit(UserViewModel vm) 
     { 
      if (ModelState.IsValid) 
      { 
       _db.SaveUser(vm.User); 
       return RedirectToAction("Index"); 
      } 
      return View(vm); 
     } 

這裏是視圖

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true)  

    @Html.HiddenFor(model => model.User.Id) 
    @Html.HiddenFor(model => model.User.UserTypeId) 

    <table> 
     <tr> 
      <td> 
       Login 
      </td> 
      <td> 
       @Html.EditorFor(model => model.User.Login) 
      </td> 
      <td> 
       Email 
      </td> 
      <td> 
       @Html.EditorFor(model => model.User.Email) 
      </td> 
      <td> 
       &nbsp; 
      </td> 
      <td> 
       &nbsp; 
      </td> 
     </tr> 
     <tr> 
      <td> 
       First Name 
      </td> 
      <td> 
       @Html.EditorFor(model => model.User.FirstName) 
      </td> 
      <td> 
       Middle Name 
      </td> 
      <td> 
       @Html.EditorFor(model => model.User.MiddleName) 
      </td> 
      <td> 
       Last Name 
      </td> 
      <td> 
       @Html.EditorFor(model => model.User.LastName) 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Role 
      </td> 
      <td> 
       @Html.DropDownListFor(m=>m.RoleId,(IEnumerable<SelectListItem>)ViewBag.RolesList, new { onchange = "adjustInterface()" }) 
       @Html.HiddenFor(m=>m.RoleId) 
      </td> 
      <td> 
       &nbsp; 
      </td> 
     </tr> 
    </table> 

    <p> 
    </p> 

    <select id="cboLocationType" onchange="adjustInterface();"> 
     <option value="0">User Should Have Access To All Practices For Vendor</option> 
     <option value="1">User Should Have Access To Specific Practices For Vendor</option> 
    </select> 
    <div id="accountSection"> 
     <h3> 
      Accounts</h3> 
     <span> 
      <input type="text" style="width: 400px" /> 
      <button>Add</button> 
     </span> 
     <p /> 
     <table> 
      <tr> 
       <td> 
        Practice #1 
       </td> 
       <td> 
        <button> 
         Remove</button> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        Practice #2 
       </td> 
       <td> 
        <button> 
         Remove</button> 
       </td> 
      </tr> 
     </table> 
    </div>  
    <p> 
     <input type="submit" value="Save" /> 
    </p> 
} 
<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 
+0

這似乎有一直是瀏覽器緩存問題。我清除了緩存並且頁面正常工作 –

回答

0

瀏覽器被緩存的結果 - 當我刷新瀏覽器的緩存,它的工作

相關問題