나는 종종 여러 장소에서 양식을 작성하고 별도의 ViewModel을 사용하여 해당 명령을 실행합니다. 하지만 프로젝트에서 높은 수준의 복제가 지나치게 두렵지는 않습니다. 이는 개인적인 취향입니다.
당신의보기 모델에 주소라는 개체가 있다고 가정하는 부분보기를 사용하여 그 이름에 바인딩 된 이름 앞에 접두사를 사용하여 제안하고 싶습니다.
보기 모델의 해당 개체는 주소가 필요한 모든보기 모델에서 사용되는 공유 개체 일 수 있습니다. 편의상 "주소"라는 유형의 "AddressViewModelComponent"유형을 가정 해 봅시다.
public class SomeViewModel
{
public string SomeFormValue
{
get;
set;
}
public string SomeOtherFormValue
{
get;
set;
}
public AddressViewModelComponent Address
{
get;
set;
}
}
public class AddressViewModelComponent
{
public string AddressLineOne
{
get;
set;
}
public string AddressLineTwo
{
get;
set;
}
// Etc
}
당신이 그것을 필요로하고 부분보기 같은 것을 포함 할 수있는 뷰 모델에 주소를 넣을 수 있습니다이 방법은 다음
<li><%= Html.TextBox("Address.AddressLineOne") %></li>
<li><%= Html.TextBox("Address.AddressLineTwo") %></li>
귀하의 실제보기 양식은 다음과 같이 보일 것입니다 :
<% using(Html.BeginForm()) %>
<ul>
<li><%= Html.TextBox("SomeFormValue") %></li>
<li><%= Html.TextBox("SomeOtherFormValue") %></li>
<% Html.RenderPartial("AddressForm"); %>
</ul>
<%} %>
기본 모델 바인더는 관련 속성을 해당 뷰 모델에 속성을 뷰 모델의 "주소"라는 개체를 찾아, 그리고 채우는 알고있다.
이것은 과도 할 수 있으며 내 프로젝트에서 양식을 "공유"해야하는 경우를 결코 발견하지 못했지만 위의 내용은 내가 할 것인지를 찾기 시작했을 것입니다.