过滤时,如何 POST 一个 SearchModel,但在视图中返回一个不同的 ResultsModel
While filtering, how to POST a SearchModel, but GET a different ResultsModel back in the view
我正在创建一个筛选视图来查找记录。 上的这个例子有帮助,但没有提到如何处理(已过滤)View
。
下面的错误是因为,操作 returns 一个 List<ProductViewModel>
,并且 Errors/complains 视图正在使用 SearchViewModel,
我需要这个 POST
searchmodel/variables,但GET
支持 list/results 模型
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[ViewModels.ProductVM]', but this
dictionary requires a model item of type 'ViewModels.SearchModel'.
Issue/Question:因为有两个模型,SearchViewModel
传递给控制器,结果返回ProductViewModel
,哪个模型应该强类型化到视图?以及如何创建视图来处理 SearchModel
和 ProductModel
如果我强烈键入 ProductVM,那么我会从 SearchVM 中松开提交表单。
我创建 SearchView
作为主视图,而 _ResultsPartialView
作为部分视图,这是错误的吗?
public ActionResult Index(SearchModel searchModel)
{
var filteredProdVMList = _Repository.GetFilteredProducts(searchModel);
return View(filteredProdVMList);
}
public class ProductVM
{
public int Id { get; set; }
public int Price { get; set; }
public string Name { get; set; }
// implicit const... blah.. removed
}
public class SearchModel
{
public int? Id { get; set; }
public int? PriceFrom { get; set; }
public int? PriceTo { get; set; }
public string Name { get; set; }
}
您需要修改 SearchModel
以包含产品属性 的集合
public class SearchModel
{
public int? PriceFrom { get; set; }
public int? PriceTo { get; set; }
....
public IEnumerable<ProductVM> Products { get; set; } // add
}
那么你return就SearchModel
你的看法
public ActionResult Filter(SearchModel filter)
{
filter.Products = _repository.GetFilteredProducts(filter);
return View(filter);
}
您的观点将是
@model SearchModel
....
@using (Html.BeginForm("Filter", "yourControllerName", FormMethod.Get))
{
@Html.LabelFor(m => m.PriceFrom)
@Html.EditorFor(m => m.PriceFrom)
@Html.ValidationMessageFor(m => m.PriceFrom)
... // other form controls for properties you want to filter the results on
<input type="submit" value="Filter" />
}
@Html.Partial("_ResultsPartialView", Model.Products)
我正在创建一个筛选视图来查找记录。 View
。
下面的错误是因为,操作 returns 一个 List<ProductViewModel>
,并且 Errors/complains 视图正在使用 SearchViewModel,
我需要这个 POST
searchmodel/variables,但GET
支持 list/results 模型
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ViewModels.ProductVM]', but this dictionary requires a model item of type 'ViewModels.SearchModel'.
Issue/Question:因为有两个模型,SearchViewModel
传递给控制器,结果返回ProductViewModel
,哪个模型应该强类型化到视图?以及如何创建视图来处理 SearchModel
和 ProductModel
如果我强烈键入 ProductVM,那么我会从 SearchVM 中松开提交表单。
我创建 SearchView
作为主视图,而 _ResultsPartialView
作为部分视图,这是错误的吗?
public ActionResult Index(SearchModel searchModel)
{
var filteredProdVMList = _Repository.GetFilteredProducts(searchModel);
return View(filteredProdVMList);
}
public class ProductVM
{
public int Id { get; set; }
public int Price { get; set; }
public string Name { get; set; }
// implicit const... blah.. removed
}
public class SearchModel
{
public int? Id { get; set; }
public int? PriceFrom { get; set; }
public int? PriceTo { get; set; }
public string Name { get; set; }
}
您需要修改 SearchModel
以包含产品属性 的集合
public class SearchModel
{
public int? PriceFrom { get; set; }
public int? PriceTo { get; set; }
....
public IEnumerable<ProductVM> Products { get; set; } // add
}
那么你return就SearchModel
你的看法
public ActionResult Filter(SearchModel filter)
{
filter.Products = _repository.GetFilteredProducts(filter);
return View(filter);
}
您的观点将是
@model SearchModel
....
@using (Html.BeginForm("Filter", "yourControllerName", FormMethod.Get))
{
@Html.LabelFor(m => m.PriceFrom)
@Html.EditorFor(m => m.PriceFrom)
@Html.ValidationMessageFor(m => m.PriceFrom)
... // other form controls for properties you want to filter the results on
<input type="submit" value="Filter" />
}
@Html.Partial("_ResultsPartialView", Model.Products)