在ASP.net MVC中:
我应该如何/可以将表格数据(从视图)传递给控制器?
这就是我的方向:
Controller Index函数将ViewModel对象传递给View.
ViewModel对象包含分页列表以及一些SelectLists._ ViewModel对象还包含一个名为theFilter的自定义类.此类的目的是通过表单保存从视图发布的过滤器信息.
我希望Index [AcceptVerbs(HttpVerbs.Post)]函数接收填充了表单数据的填充对象,以及页码(就像现在一样)
以下是我的代码片段:
控制器/索引回发功能:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(int? page, CaseFilter caseFilter) { const int pageSize = 10; var cases = caseRepo.FindAllCases(); var paginatedCases = new PaginatedList(cases, page ?? 0, pageSize); return View(new CaseIndexViewModel(paginatedCases, caseFilter)); }
过滤类:
public class CaseFilter { int iVolume_id = 0, iSubject_id = 0; public CaseFilter() { } public int Volume_id { get { return iVolume_id; } set { iVolume_id = value; } } public int Subject_id { get { return iSubject_id; } set { iSubject_id = value; } } }
而ViewModel类:
public class CaseIndexViewModel { public PaginatedListPaginatedCases { get; private set; } public CaseFilter CaseFilter { get; private set; } public CaseIndexViewModel(PaginatedList paginatedCases, CaseFilter caseFilter) { PaginatedCases = paginatedCases; CaseFilter = caseFilter; } }
基本上我试图避免使用Request.Form来填充Filter类,至少不要在Controller中使用它.
欢迎任何帮助,建议或推论!
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(FormCollection collection) { string valueFromNameTextBox = collection["name"]; }
您可以使用表单上所有输入的名称为此集合建立索引.