2016-11-21 1 views
0

내가하려는 것은 한 페이지에서 새 하우스를 만들고 기존의 모든 하우스를 디플로 이스하는 것입니다. 기본보기에 대한 코드는 다음과 같습니다데이터를 새로 삽입하고 목록보기

@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.Imumerable`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; } 
    } 

처럼 그리고 클래스에서 하나 개의 새로운 속성 만들기 검사 모델이 부분 뷰에서 null이 아닙니다.

@if (Model != null) 
{ 

} 
+0

정말 감사드립니다. – Fahad

관련 문제