2016-11-21 9 views
0

私がしようとしているのは、1つのページで新しいHouseを作成し、既存のすべての家を複製することです。 メインビューのコードは次のとおりです。新規にデータを挿入して一覧表示する

@modelData.Models.SGHouse 

@{ 
    ViewBag.Title = ""; 
} 

<h2>Create</h2> 


@using (Ajax.BeginForm(new AjaxOptions 
             { 
             HttpMethod = "Post", 
             InsertionMode = InsertionMode.Replace, 
             UpdateTargetId = "HouseList" 
            })) 
     { 
      @Html.AntiForgeryToken() 

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

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

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 
@Html.Partial("_Houses",Model) 

すべての家をリストする私のパーシャルビューのコードは次のとおりです。私がしなければ

@model IEnumerable<Data.Models.SGHouse> 
<div id="HouseList"> 

    <table class="table"> 
     <tr> 
      <th> 
       @Html.DisplayNameFor(model => model.House) 
      </th> 
      <th></th> 
     </tr> 

     @foreach (var item in Model) 
     { 
      <tr> 
       <td> 
        @Html.DisplayFor(modelItem => item.House) 
       </td> 
       <td> 
        @Html.ActionLink("Edit", "Edit", new { id = item._id }) | 
        @Html.ActionLink("Details", "Details", new { id = item._id }) | 
        @Html.ActionLink("Delete", "Delete", new { id = item._id }) 
       </td> 
      </tr> 
     } 

    </table> 
</div> 

私は、私のコントローラから復帰して何を見るかわかりません:

public ActionResult Create() 
      { 
       return View(); 
      } 

モデルについての私の部分的な見解からエラーが発生します。私が使用している場合は :

public ActionResult Create() 
       { 
        return View(new SGHouse()); 
       } 

その後、私はエラー

を取得し、辞書に渡されるモデルアイテムはタイプ「Data.Models.SGHouse」であるが、この辞書はタイプ「システムのモデルアイテムが必要です。 Collections.Generic.IEnumerable`1 [Data.Models.SGHouse] '。

これを解決する方法については、何か助けてください。

おかげ

+0

は初めての新規作成用ですか?それとも作成と編集の両方のためですか? –

答えて

2

だけにしてください。この

@Html.RenderPartial("_Houses", Model.ListSGHouse); 

ような呼び出しビューでConstroller

public ActionResult Create() 
    { 
     SGHouse model = new SGHouse(); 
     List<SGHouse> LstSGHouse = new List<SGHouse>(); 
     LstSGHouse = //Just add list of SGHouse to this property 
     model.ListSGHouse = LstSGHouse; //Assign above to this 


     return View(model); 
    } 

この

public class SGHouse 
    { 

     public List<SGHouse> ListSGHouse { get; set; } 
    } 

好きで、クラス内の1つの新しいプロパティを作成します。チェックモデルが部分ビューでヌルではありません

@if (Model != null) 
{ 

} 
+0

ありがとう、本当に感謝します。 – Fahad

関連する問題