2017-02-17 30 views
0

我从内存数据填充DropDownList并在POST上获取此错误。具有'位置'键的ViewData项的类型为'System.String',但必须是'IEnumerable <SelectListItem>'的类型'

具有'位置'键的ViewData项的类型是'System.String',但必须是'IEnumerable'类型。

型号:

public class StaffModel 
{ 
    public int id { get; set; } 
    public string Email { get; set; } 
    [DataType(DataType.Password)] 
    public string Password { get; set; } 
    [DataType(DataType.Password)] 
    public string PasswordConfirm { get; set; } 
    public string Emp_Name { get; set; } 
    public string Emp_Address { get; set; } 
    public string Phone { get; set; } 
    public string Position { get; set; } 
    public List<SelectListItem> Positions { set; get; } 

} 

控制器:

public ActionResult Register() 
    { 

     IEnumerable<SelectListItem> position = db.Positions.Select(p => new SelectListItem 
     { 
      Text = p.Position_Title, 
      Value = p.Position_ID.ToString() 
     }); 
     ViewBag.Position = position; 
     return View(); 
    } 

    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(StaffModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
      var result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 
       Employee em = new Employee 
       { 
        Employee_Name = model.Emp_Name, 
        Address = model.Emp_Address, 
        Phone = model.Phone, 
        Position_ID = Convert.ToInt32(db.Positions.Where(p => p.Position_Title == model.Position).Select(p => p.Position_ID)), 
       }; 
       db.Employees.Add(em); 
       db.SaveChanges(); 
       return RedirectToAction("Index", "Employees"); 
      } 

     } 

     return View(model); 
    } 
enter code here 

HTML /剃刀:

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

我道歉,我重读这发生在后期..你必须在'HttpGet'和'HttpPost'行为方法中声明'ViewBag.Position',很可能在你的'if'语句之上 –

+0

我也对你的.Where语句感到困惑..'p.Position_Title'不应该等于'model.Position',因为'model.Position'将是下拉列表的**值** ..不是文本..所以基本上你的意思是'.Where(p => p.Position_Title == p.Position_ID.ToString())' –

+0

@BviLLe_Kid我该如何解决它? – bao4461826

回答

0

您在下拉列表中的绑定看起来不正确。

您是模型包含位置,这是您的下拉数据。你也有“位置”,我认为这是一个字符串,将被绑定到下拉列表中选定的值。

您的视图更改为

@Html.DropDownListFor(x=> x.Position, Model.Positions, new {@class = "form-control"})) 

然后在您的文章,它看起来好像你正试图获得通过模型的列表上的“位置”执行LINQ查询从下拉列表中选择值下降这是用来填充你的下拉菜单。您现在应该在模型的“位置”属性中具有选定的值。

所以,你应该说

Position_ID = model.Position 

我还使用一个模型为您注册查看。 类似...

public class RegisterViewModel 
     { 
      public IEnumerable<SelectListItem> Positions { get; set; } 
      public string Position { get; set; } 
     } 

加上您需要的其他字段。

在您的注册操作方法中,填充视图模型并返回视图。

public ActionResult Register() 
     { 
      var regModel = new RegisterViewModel 
      { 
       Positions = db.Positions.Select(p => new SelectListItem 
       { 
        Text = p.Position_Title, 
        Value = p.Position_ID.ToString() 
       }) 
      }; 

      return View("Register",regModel); 
     } 

您现在不需要使用ViewBag。

希望有帮助

+0

仍然没有运行 – bao4461826

+0

我刚刚编辑了答案。看你如何继续。 – Wheels73

+0

'Position_ID = model.Position'错误的类型 – bao4461826

0

它工作正常。谢谢大家帮我:d

型号:

public class StaffModel 
{ 
    public string Position { get; set; } 
    public List<Position> Positions { set; get; } 
    public int selectID { get; set; } 

} 

控制器:

public ActionResult Register() 
    { 
     StaffModel st = new StaffModel(); 
     st.Positions = db.Positions.ToList(); 
     return View(st); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Register(StaffModel model) 
    {   
     if (ModelState.IsValid) 
     { 

      var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
      var result = UserManager.Create(user, model.Password); 
      if (result.Succeeded) 
      { 
       Employee em = new Employee 
       { 
        Employee_Name = model.Emp_Name, 
        Address = model.Emp_Address, 
        Phone = model.Phone, 
        Position_ID = model.selectID, 
        ID_User = user.Id 
       }; 
       db.Employees.Add(em); 
       db.SaveChanges(); 

       return RedirectToAction("Index", "Employees"); 
      } 

      else 
       AddErrors(result); 
     } 
     ViewBag.Position = new SelectList(db.Positions, "Position_ID", "Position_Title"); 
     return View(model); 
    } 

HTML /剃刀:

<div class="form-group"> 
     @Html.LabelFor(model => model.Position, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(model=>model.selectID,new SelectList(Model.Positions, "Position_ID", "Position_Title"), htmlAttributes: new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.Position, "", new { @class = "text-danger" }) 
     </div> 
    </div> 
相关问题