2013-08-15 3 views
0

CustomerIndexViewModel을 사용하여 고객 목록, 요청이 걸린 시간 및 기타 많은 정보로 페이지를 채우는 Customer 인덱스 페이지가 있습니다.뷰 모델의 일부를 컨트롤러에 전달

CustomerIndexViewModel 내에 CustomerSearchArgsModel이 있습니다. 나는 CustomerSearchArgsModel의 고객 컨트롤러의 지수 (POST) 메소드 값으로 입력 반환 할

@model CustomerIndexViewModel 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

@using (Html.BeginForm("Index","Customer",FormMethod.Post, new { id="searchSubmit"})) 
{ 
    @Html.LabelFor(model => model.CustomerSearchArgsModel.ConsumerID) 
    @Html.EditorFor(model => model.CustomerSearchArgsModel.ConsumerID) 
    @Html.LabelFor(model => model.CustomerSearchArgsModel.LastName) 
    @Html.EditorFor(model => model.CustomerSearchArgsModel.LastName) 
    @Html.LabelFor(model => model.CustomerSearchArgsModel.FirstName) 
    @Html.EditorFor(model => model.CustomerSearchArgsModel.FirstName) 

    <input type="submit" value="Search" /> 
} 

- 내 고객 인덱스 페이지에서

public class CustomerIndexViewModel : BaseIndexViewModel 
{ 
    public IEnumerable<Customer> Customers{ get; set; } 
    public double RequestTime { get; set; } 
    public CustomerSearchArgsModel CustomerSearchArgsModel { get; set; } 
    public OtherTypes OtherType {get;set;} 
} 


public class CustomerSearchArgsModel 
{ 
    public string CustomerID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

내가 좋아하는 뭔가를 갖고 싶어.

그러나 페이지 상단에 정의 된 모델과 다른 모델을 반환하는 방법을 모르겠습니다.

답변

2

"searchSubmit"양식을 부분보기 안에 넣을 수 있습니다. 그런 다음 model.CustomerSearchArgsModel을 부분보기에 전달합니다. model.CustomerSearchArgsModel이 null이 아닌지 확인하십시오. 그렇지 않으면 예외가 발생합니다.

@Html.Partial("_search", model.CustomerSearchArgsModel) 

인덱스 페이지 부분 _search보기

@model CustomerSearchArgsModel 
@using (Html.BeginForm("Index","Customer",FormMethod.Post, new { id="searchSubmit"})) 
{ 
    @Html.LabelFor(model => model.ConsumerID) 
    @Html.EditorFor(model => model.ConsumerID) 
    @Html.LabelFor(model => model.LastName) 
    @Html.EditorFor(model => model.LastName) 
    @Html.LabelFor(model => model.FirstName) 
    @Html.EditorFor(model => model.FirstName) 

    <input type="submit" value="Search" /> 
} 

이 방법의 문제는 당신이 ConsumerID에 대한 텍스트 상자에 표시되는 0 값을 얻을 것입니다. 이 문제를 해결하려면 Html.Partial 대신 Html.Action을 사용할 수 있습니다.

희망이 도움이됩니다.

+0

감사합니다. – tom

관련 문제