2014-12-23 1 views
0

, 나는 함께 세 가지 간단한 데이터 모델 뒀다 : 여기부분 뷰에 전달 된 데이터 모델을 유지하는 방법은 무엇입니까? 내가 직면하고있는 문제를 설명하기 위해

public class PersonalModel { 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

public class AddressModel { 
    public string Street { get; set; } 
} 

public class OverallModel { 
    public string Id { get; set; } 
    public PersonalModel Personal { get; set; } 
    public AddressModel Address { get; set; } 
} 

을 내 간단한 Index.chtml :

@model WebAppTest.Models.OverallModel 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
    <h4>OverallModel</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    @Html.HiddenFor(model => model.Id) 

    <div> 
     @Html.Partial("Personal", Model.Personal) 
    </div> 
    <div> 
     @Html.Partial("Address", Model.Address) 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Save" class="btn btn-default" /> 
     </div> 
    </div> 
    </div> 
} 

(가) 버튼을 클릭 "저장"을 다음 컨트롤러 메서드가 호출됩니다 :

[HttpPost] 
public ActionResult Index(OverallModel model) { 
    return View(model); 
} 

model.Personalmodel.Address 값은 항상이야 내가 가진 문제가 있음 얼마나 컨트롤러 메서드에서 null로.

값이 부분 뷰로 올바르게 전달되고 있지만 제출을 클릭하면 Razor 엔진이 전체 개체를 다시 묶을 수 없습니다.

내가 누락 된 부분이 무엇인지 내게 가르쳐 주시면 감사하겠습니다. 문안 인사.

답변

6

OverallModel에 게시 할 컨트롤의 이름이 올바르게 지정되도록 OverallModel을 부분으로 전달하십시오. 현재 당신이 name="FirstName"와 컨트롤을 할 것이다 그러나 그들은 다른 방법으로

@model OverallModel 
.... 
@Html.TextBoxFor(m => m.Personal.FirstName) 

에 맞게 name="Personal.FirstName"

@Html.Partial("Personal", Model) 

을해야하며 파셜을 변경해야합니다, 당신은 또한 부분

@Html.Partial("Address", Model.Personal, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Personal" }}) 
의 접두사를 전달할 수 있습니다
+0

감사합니다. 이 트릭이 작동하는 것 같습니다. – Peter

0

나는 너와 똑같은 문제가 있었기 때문에 같은 문제가있는 누군가를 위해 여기에 2 센트를 넣을 것이다.

내 솔루션은 EditorTemplates를 사용합니다.

모델을 Personal, Address, Overal라고도합니다.

Overal 모델의 인스턴스를 만들고 Get 메서드에 View 및 Post 메서드로 보내면받는 매개 변수로 넣습니다.

public ActionResult Index() 
    { 
     var model = new OveralModel(); 

     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(OveralModel model) 
    { 
     return View(model); 
    } 

색인 페이지는 (내가 당신을 사용합니다)

@model EditorFor.Models.OveralModel 

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

    <div class="form-horizontal"> 
     <h4>OverallModel</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @Html.HiddenFor(m => m.Id) 

     <div>    
      @Html.EditorFor(m => m.Personal) 
     </div> 
     <div> 
      @Html.EditorFor(m => m.Address) 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

우리가 EditorModels을 사용할 수 있습니다 PartialViews의 Istead, 나는 우리가 원하는 때마다 우리가 편집기로 사용할 수있는 PartialView로 이해 같이 표시됩니다.

그래서 이것을 사용하기 위해 뷰/공유에 EditorTemplates라는 폴더를 만들고 모델과 동일한 이름으로 뷰를 만듭니다. 우리는 우리의 소우 @Html.EditorFor(m => m.Personal)에 쓸 때 그래서 EditorTemplates에 모양과 검색합니다 우리의 PersonalModel에 대한 예를 들어 EditorTemplate를 들어

는 PersonalModel.cshtml 호출됩니다과의 코드는이

@model EditorFor.Models.PersonelModel 


<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> 

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

같은 것을 볼 수 있습니다 같은 이름의 EditorTemplate을 사이트에서 사용하십시오.

Index site will look like this

하고 전송 버튼을 클릭하면 그것은 PS

Looks like it worked and we got our data back from view and use it for whatever we want to.

다시 컨트롤러로 전체 모델을 다시 보내드립니다 : 내 첫 번째 게시물, 그래서 내가 그것을 이해할 수있는 희망과 도움이 될 것입니다 다른 사람들은 현재와 미래에도 그렇습니다. 좋은 하루 되세요

관련 문제