2017-02-14 35 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设置,所以它是没有意义的,它将是空的。 –

相关问题