我有一个名为MainViewModel的ViewModel,在其中我有MainFilterViewModel类型的属性。是否可以让我的操作与public ActionResult actionName(MainFilterViewModel filter)
一样并返回return View(new MainViewModel(){...})
,这样我就不必添加整个ViewModel类了?
public class MainViewModel
{
public MainFilterViewModel Filter {get;set;}
public List<GridItem> Rows {get;set;}
}
public class MainFilterViewModel
{
public string Name {get;set;}
public String City {get;set;}
}
查看(仅举例说明)
@using (Html.BeginForm("ActionName", "TheController", FormMethod.Post, new { Id = "formPrincipal" }))
{
@Html.TextBoxFor(m => m.Filter.Name)
@Html.TextBoxFor(m => m.Filter.City)
<button type="submit" class="btn btn-success" id="btnFilter" value="Filter" />
}
<div id="data-table">
@* Looping the Rows collection *@
</div>
控制器
public ActionResult ActionName(MainFilterViewModel filter)
{
// Request data from repository based on filter parameter
var model = new MainViewModel();
// fill model with info from the repository
return View(model);
}
BTW,我尝试搜索模型内部类型/属性的动作参数,但没有找到任何内容。