2017-02-14 40 views
0

我有一個用戶管理的人控制器,我試圖弄清楚如何在用戶從編輯頁面提交時獲取下拉選項。每當我點擊頁面提交時,視圖模型中的任何值都不會傳遞到該帖子。我無法從下拉菜單中獲得他們選擇的價值來設置角色。從視圖模型中的下拉菜單中獲取提交的數據?

見下面的視圖模型:

public class PersonViewModel 
{ 
    public int PersonId { get; set; } 
    [Display(Name = "Full Name")] 
    public string FullName { get; set; } 
    public string Email { get; set; } 
    [Display(Name = "Current Role")] 
    public string SetRole { get; set; } 
    public List<RoleListViewModel> Roles { get; set; } 
} 

見下控制器編輯功能:

// GET: People/Edit/5 
    public ActionResult Edit(int? id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     Person person = db.people.Find(id); 
     if (person == null) 
     { 
      return HttpNotFound(); 
     } 
     PersonViewModel pvm = new PersonViewModel(); 
     List<IdentityRole> roles = adb.Roles.ToList(); 
     var rlvm = new List<RoleListViewModel>(); 
     roles.ForEach(x => rlvm.Add(new RoleListViewModel { RoleId = x.Id, RoleName = x.Name })); 
     pvm.PersonId = person.PersonId; 
     pvm.FullName = person.FirstName + " " + person.LastName; 
     pvm.Email = person.Email; 
     pvm.Roles = rlvm; 
     ViewBag.RoleList = new SelectList(rlvm, "RoleName", "RoleName", person.CurrentRole); 

     return View(pvm); 
    } 

    // POST: People/Edit/5 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Edit(PersonViewModel pvm) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Entry(pvm).State = EntityState.Modified; 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     var usr = new AccountController(); 
     var pers = db.people.Where(x => x.PersonId == pvm.PersonId).FirstOrDefault(); 
     usr.UserManager.AddToRoleAsync(pers.NetId, /* their choice should go here but how? */); 
     db.SaveChanges(); 


     return View(pvm); 
    } 

這裏是CSHTML:

<div class="form-group"> 
     @Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="form-control-static"> 
      @Html.DropDownList("RoleList", null, new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.Roles, "", new { @class = "text-danger" }) 
     </div> 
    </div> 
+0

您綁定到名爲'RoleList'的屬性,該屬性在模型中不存在。我假設你想'@ Html.DropDownListFor(m => m.SetRole,(SelectList)ViewBag.RoleList)'然後你需要刪除'SelectList'構造函數中的最後一個參數(如果你想選擇一個選項,那麼你需要設置'SetRole'的值)。你的標籤和驗證信息應該綁定到'SetRole'而不是'Roles'。既然你有一個視圖模型,'Roles'應該是'Enumerable '的類型,並且除掉'VIewBag'。 –

+0

我建議你學習代碼[這裏](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but -must-be-o) –

回答

1

做一個變量在您的視圖模型存儲選定的值然後在視圖中使用

@Html.DropDownListFor(m => m.SelectedRoleVariable, RolesSelectList, new { @class = "form-control" }); 
+0

他可以使用SetRole變量,因爲他可能設置當前角色 – Fran

+0

RolesSelectList從哪裏來?我認爲這是一個選擇列表,但我在控制器中創建了它,並將其分配給ViewBag.RoleList –

+0

我使用model.rlvm在cshtml中創建了一個新的選擇列表,但當我點擊提交時,出於某種原因,pvm.Email屬性爲null。爲什麼?我調試,我可以看到person.Email設置,所以它是沒有意義的,它將是空的。 –

相關問題