如何在 .NET MVC 中将表单值传递给控制器
在 ASP.net MVC 中:
我应该/可以如何将表单数据(从视图)传递到控制器?
这就是我的方向:
- 控制器索引函数将 ViewModel 对象传递给视图。
- ViewModel 对象包含一个分页列表以及一些 SelectList。 _ ViewModel 对象还包含一个我命名为 theFilter 的自定义类。 此类的目的是保存通过表单从视图发布的过滤器信息。
- 我希望 Index [AcceptVerbs(HttpVerbs.Post)] 函数接收填充有表单数据的 Filter 对象以及页码(现在是这样)
以下是我的代码片段:
Controller/Index 回发函数:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int? page, CaseFilter caseFilter)
{
const int pageSize = 10;
var cases = caseRepo.FindAllCases();
var paginatedCases = new PaginatedList<Case>(cases, page ?? 0, pageSize);
return View(new CaseIndexViewModel(paginatedCases, caseFilter));
}
Filter 类:
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 PaginatedList<Case> PaginatedCases { get; private set; }
public CaseFilter CaseFilter { get; private set; }
public CaseIndexViewModel(PaginatedList<Case> paginatedCases, CaseFilter caseFilter)
{
PaginatedCases = paginatedCases;
CaseFilter = caseFilter;
}
}
基本上,我试图避免使用 Request.Form 来填充 Filter 类,至少不在控制器中使用它。
欢迎任何帮助、建议或批评!
In ASP.net MVC:
How should/Can I pass Form data (From the View) to the Controller?
This is the way I am heading :
- The Controller Index function is passing a ViewModel object to the View.
- The ViewModel object contains a paginated list as well as some SelectLists.
_ The ViewModel object also contains a custom class I named theFilter. This class' purpose is to hold the Filter information Posted from the View via a Form. - I want the Index [AcceptVerbs(HttpVerbs.Post)] function to receive theFilter object populated with the form data, as well as the page number (as is it right now)
Here are snippets of my code:
The Controller/Index postback function:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int? page, CaseFilter caseFilter)
{
const int pageSize = 10;
var cases = caseRepo.FindAllCases();
var paginatedCases = new PaginatedList<Case>(cases, page ?? 0, pageSize);
return View(new CaseIndexViewModel(paginatedCases, caseFilter));
}
The Filter Class:
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; } }
}
And the ViewModel class:
public class CaseIndexViewModel
{
public PaginatedList<Case> PaginatedCases { get; private set; }
public CaseFilter CaseFilter { get; private set; }
public CaseIndexViewModel(PaginatedList<Case> paginatedCases, CaseFilter caseFilter)
{
PaginatedCases = paginatedCases;
CaseFilter = caseFilter;
}
}
Basically I am trying to avoid using Request.Form to populate the Filter class, at least not to use it within the Controller.
Any help, suggestions or disses are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用表单上所有输入的名称对此集合进行索引。
You can index into this collection with the names of all the inputs on the form.
我没有使我的方法签名复杂化,而是开始使用 Controller 中的 ValueProvider 属性和 Try/UpdateModel 来检索表单/路由值,除非这些值是简单的属性。 另一方面,我也可能不会将过滤器作为视图模型的一部分——我倾向于对页面模型有更狭隘的概念,希望它成为业务模型,而不是页面上所有数据的模型——并且只需通过 ViewData 传递过滤器值。
Rather than complicate my method signatures, I've taken to using the ValueProvider property and Try/UpdateModel in the Controller to retrieve form/route values unless the values are simple properties. On the other hand, I would probably also not make the filter part of the model for the View -- I tend to have a narrower conception of the model for the page, wanting it rather to be the business model rather that a model of all the data on the page -- and would simply pass the filter values via ViewData.
为了扩展 BFree 的答案,您可以通过执行以下操作来遍历表单中的所有元素:
如果 key.contains if 的元素太多,它可能会变得有点难看,所以要小心;)。
To expand BFree's answer, you can go through all the elements in the form by doing something like this:
If it has too many elements for the key.contains if, it can get a bit ugly though, so beware ;).
最后,我什至不需要使用请求集合。 当我将 CaseFilter 对象设置为
上面的代码中的参数时,它会自动填充。
Finally, I do not need to even use the Request Collection. The CaseFilter object is filled automatically as I set it as a parameter in
The code above works as it is.