2016-11-21 37 views
0

我想要做的是有一個頁面來創建新的房子,也diplsay所有現有的房屋。 主視圖代碼:在onew中插入和列出數據查看

@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(); 
      } 

我從模型的部分視圖中得到錯誤爲null。 如果我使用:

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

然後我得到錯誤

傳遞到字典的模型產品類型「Data.Models.SGHouse」,但這部詞典需要類型「系統的典範項目。 Collections.Generic.IEnumerable`1 [Data.Models.SGHouse]」。

任何幫助如何解決此問題。

感謝

+0

是您的第一個視圖,僅用於創建新視圖嗎?還是用於創建和編輯? –

回答

2

創建類一個新的物業這樣

public class SGHouse 
    { 

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

而且在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); 
    } 

在View只需要調用這樣

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

確保檢查模型在您的局部視圖中不爲空

@if (Model != null) 
{ 

} 
+0

非常感謝,非常感謝。 – Fahad

相關問題