2016-04-06 17 views
-2

私は何をしようとしているのか分かりません。MVC - 辞書に渡されたモデルアイテムは..型ですが、この辞書にはモデルアイテムが必要です

辞書に渡されるモデル項目は、 'System.Collections.Generic.List`1 [Interface.Models.UserAccount]'型ですが、この辞書には 'Interface.Models.UserAccount'型のモデル項目が必要です。

Index.cshtml

@model Interface.Models.UserAccount 

@{ 
    ViewBag.Title = "Home Page"; 
} 

<h2>Register</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 
    <h4>Create an Account</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

HomeController.cs

namespace Interface.Controllers 
{ 
    public class HomeController : Controller 
    { 
     // GET: Account 
     public ActionResult Index() 
     { 
     SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString); 
      MyDB db = new MyDB(); 
      { 
      return View(db.userAccount.ToList()); 
      } 
     } 
     [HttpPost] 
     public ActionResult Index(UserAccount account) 
     { 
     if (ModelState.IsValid) 
     { 
      MyDB db = new MyDB(); 
      { 
       db.userAccount.Add(account); 
       db.SaveChanges(); 
      } 
      ModelState.Clear(); 
      ViewBag.Message = account.FirstName + " " + account.LastName + "successfully registered."; 
     } 
     return View(); 
    } 

元/ホーム/インデックスページは、そう私は何をしようとしていたことは/アカウントを交換して、自分のアプリケーションのための冗長でした/ Register/Account/Registerが実際には/ Home/Indexになるように登録します。

問題は、前にビューの一部であったアイテムを返すところにあり、それが好きではありません。

このページが正しく読み込まれるように修正する方法を教えてください。私は、同様の結果の多くを見るが、私は

+0

メッセージは自己説明的です。あなたは '@model UserAccount'(単一のオブジェクト)を持っていますが、コントローラメソッドはオブジェクトのコレクションを返しています –

答えて

0

あなたは代わりに、1つのユーザーアカウント のユーザーアカウントのリストを渡すあなたが唯一のユーザーアカウントを渡す必要がないときには、例えば、自分のコード内のIEnumerableを持っている:

db.userAccount.ToList().First() 
関連する問題