2017-02-17 66 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> 
相關問題