2017-02-17 7 views
0

メモリ内のデータからDropDownListを取り込み、POSTにこのエラーが発生しています。'Position'キーを持つViewDataアイテムは 'System.String'タイプですが、 'IEnumerable <SelectListItem>'タイプである必要があります

キー 'Position'を持つ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'がドロップダウンリストの** value **になるので決して' model.Position'と同じでなければなりません..テキストではありませんので、基本的に '.Where(p =>) p.Position_Title == p.Position_ID.ToString()) ' –

+0

@BviLLe_Kidどうすれば修正できますか? – bao4461826

答えて

0

ドロップダウンリストのバインディングが正しく表示されません。

モデルには、ドロップダウンのデータである位置が含まれています。また、ドロップダウンで選択した値にバインドされる文字列が「位置」であると想定しています。あなたは、モデルのリスト「位置」にLINQクエリを実行することで、ドロップダウンから選択した値を取得しようとしているかのように

はあなたのポストに続いて

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

にあなたのビューを変更

、それが見えますドロップダウンに値を設定するために使用されました。これで、モデルの「位置」プロパティに選択した値が設定されます。

だから、私はまた、あなたの登録を表示するためのモデルを使用したい

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> 
関連する問題