2011-04-13 2 views
1

문제를 사용하여 : 내가 주소에 대한 편집 템플릿에 입력 한 정보를 손실하고 양식을 제출데이터 손실 EditorFor

.

나는 다음과 같은 모델을 가지고 :

주소는 다음과 같이 정의된다
public class MoveRecord 
{ 
    public Address StartPoint { get; set; } 
    public Address EndPoint { get; set; } 
} 

:

[HttpPost] 
public ActionResult Edit(MoveRecord model) 
{ 
    if (ModelState.IsValid) 
    { 
     //Save info 
    } 

    return View(model); 
} 

내 편집보기가 사용 :

다음
public class Address 
{ 
    public String City { get; set;} 
    public String State { get; set; } 
    public String Line1{ get; set; } 
    public String PostalCode { get; set; } 
} 

내 행동의 방법이다

using (Html.BeginForm("Edit", "Move", FormMethod.Post)) 
{ 
    @Html.EditorFor(m => m.StartPoint); 
    @Html.EditorFor(m => m.EndPoint); 
    <input type="submit" value="Save" /> 
} 

내 편집 템플릿은 다음과 같습니다

<table class="form address"> 
    <tbody> 
     <tr> 
      <th style="width: 200px;"> 
       @Html.LabelFor(m => m.Line1): 
      </th> 
      <td> 
       @Html.TextBoxFor(m => m.Line1, new { style = "width: 300px;" }) 
      </td> 
     </tr> 
     <tr id="zip"> 
      <th> 
       @Html.LabelFor(m => m.PostalCode): 
      </th> 
      <td> 
       @Html.TextBoxFor(m => m.PostalCode, new { style = "width: 150px;" }) 
      </td> 
     </tr> 
     <tr id="city"> 
      <th> 
       @Html.LabelFor(m => m.City): 
      </th> 
      <td> 
       @Html.TextBoxFor(m => m.City, new { style = "width: 150px;" }) 
      </td> 
     </tr> 
     <tr id="state"> 
      <th> 
       @Html.LabelFor(m => m.StateProvince): 
      </th> 
      <td> 
       @Html.TextBoxFor(m => m.StateProvince, new { style = "width: 150px;" }) 
      </td> 
     </tr> 
    </tbody> 
</table> 

모든 것이 잘 렌더링, I 양식 I는 주소 필드에 입력 한 정보를 포함하지 않는 경우의 조치 방법에 들어갈 모델을 제출하지만 때. 모든 것을 동일한보기로 옮기면 잘 작동하지만 편집기 템플릿을 사용하여 내보기를 읽기 쉽게 유지할 수 있습니다. 모델에 올바르게 바인딩 된 편집기 템플릿에서 데이터를 가져올 수 있습니까?

편집 : 게시 내 액션 메소드

+0

조치 방법 서명은 어떻게됩니까? –

+0

원래 질문에 내 Action 메서드를 추가했습니다. –

답변

0

액션이 같이 있어야 할 양식을 제출 : 당신은 두 개의 주소에 직접 결합하려는 경우

[HttpPost] 
public ActionResult Edit(MoveRecord model) 
{ 
    // model.StartPoint and model.EndPoint should be correctly bound here. 
    ... 
} 

가 아니면 필요 올바른 접두어를 설정하십시오.

[HttpPost] 
public ActionResult Edit(
    [Bind(Prefix = "StartPoint")] Address start, 
    [Bind(Prefix = "EndPoint")] Address end 
) 
{ 
    ... 
} 
+0

첫 번째 대답의 의미를 명확히 할 수 있습니까? 당신은 어떻게 올바르게 바인딩합니까? – BlueChippy