我将我的 Web 应用程序从预览版 3 转换为 beta1,现在正尝试使用框架的新功能。其中之一是模型绑定(bind)。
对于这种特殊情况,我创建了一个类(目前)只是一堆简单类型属性的容器。
如果我用一堆文本框创建一个表单,我希望框架用这些字段填充一个 SearchBag 实例。
开箱即用还是我实现
SearchBagBinder?我快速浏览了一下
在 IModelBinder 但不能完全
把我的头绕在它周围
DefaultModelBinder 源没有
让我变得更聪明。
通过表格,而不是通过输入
直接网址?
这方面的信息(大多数博客是
过时)?我以为我读了一个帖子
菲尔一次,但我不能
似乎再也找不到了。
任何帮助表示赞赏。
最佳答案
- Where do I start? Is this behaviour out of the box or do I implement a SearchBagBinder? I had a quick look at the IModelBinder but can't quite wrap my head around it and the DefaultModelBinder source doesn't make me any the wiser.
它是开箱即用的。您可以使用 UpdateModel 或 ModelBinder 来实现您想要做的事情。
- What is this ModelBindingContext?
这包含将请求绑定(bind)到您的模型的所有必要信息。与 ControllerContext 和 ActionFilterContext 类似,它基本上是 ModelBinder 的状态,并且包含执行所需操作所需的所有信息,前提是您遵循 ASP.NET MVC 团队对 ModelBinder 应该做什么的建议。
- How do I access my form fields?
context.HttpContext.Request.Forms["myformfield"];
或者
foreach (var field in context.HttpContext.Request.Forms.Keys) {
var value = context.HttpContext.Request.Forms[field];
}
- What if the values are not passed on by a form but rather by entering a URL directly?
如果您需要同时检查 Form 和 QueryString ,只需遍历这两个集合。
foreach (var field in context.HttpContext.Request.Forms.Keys) {
var value = context.HttpContext.Request.Forms[field];
}
foreach (var field in context.HttpContext.Request.QueryStrings.Keys) {
var value = context.HttpContext.Request.QueryStrings[field];
}
或者您可以循环访问包含 Form、QueryString、Headers 等的 Param。
foreach (var field in context.HttpContext.Request.Params.Keys) {
var value = context.HttpContext.Request.Params[field];
}
- Where do I find up-to-date information on this*(most blogs are outdated)? I thought I read a post by Phill at one time, but I can't seem to find it no more.
你说得对,Phil 是作为 ASP.NET MVC 的 PM 获取信息的最佳场所。
关于asp.net-mvc - asp.net mvc Beta1中的模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/271745/