asp.net-mvc ASP.Net MVC 自定义模型绑定解释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1193453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.Net MVC Custom Model Binding explanation
提问by Richbits
Is anyone aware of where I can find a tutorial for building a custom model binder.
有谁知道我在哪里可以找到构建自定义模型绑定器的教程。
I have found several issues using the default model binder with EntitySets (not populating when creating new objects and creating duplicates on editing objects) and find that my UI doesn't match my model exactly (e.g. time fields formated "hh:mm am" do not match with a timespan). I therefore want to look at whether creating a custom model binder is a better solution. I have found a couple of examples: hereand here. The first is I believe out of date and the second doesn't really explain what is going on.
我发现使用带有 EntitySets 的默认模型绑定器存在几个问题(在创建新对象时不填充并在编辑对象上创建重复项),并发现我的 UI 与我的模型不完全匹配(例如格式为“hh:mm am”的时间字段)与时间跨度不匹配)。因此,我想看看创建自定义模型绑定器是否是更好的解决方案。我找到了几个例子:here和here。第一个是我认为已经过时,第二个并没有真正解释发生了什么。
My main problem is that I don't fully understand what the ControllerContext and ModelBindingContext Of the BindModel method actually are, i.e. how they relate to the controller and model.
我的主要问题是我不完全理解 BindModel 方法的 ControllerContext 和 ModelBindingContext 实际上是什么,即它们与控制器和模型的关系。
I would be grateful if someone point me to a tutorial or could take a look at the dave hayden's post and show me how it might look with the current implementation of MVC.
如果有人给我指点教程或者可以看看dave hayden的帖子并向我展示它与当前 MVC 实现的外观,我将不胜感激。
采纳答案by Richbits
I've actually now found the following two posts which a good summary of model binding:
我现在实际上发现了以下两篇文章,它们很好地总结了模型绑定:
http://odetocode.com/Blogs/scott/archive/2009/04/27/12788.aspx
http://odetocode.com/Blogs/scott/archive/2009/04/27/12788.aspx
http://odetocode.com/Blogs/scott/archive/2009/05/05/12801.aspx
http://odetocode.com/Blogs/scott/archive/2009/05/05/12801.aspx
回答by mkchandler
Here's a good explanation of ModelBinders that I believe will cover your questions: http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx
这是对 ModelBinders 的一个很好的解释,我相信它会涵盖您的问题:http: //www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx
A quick Google searchpulled up a few others... I will let you filter through them :-)
一个快速的谷歌搜索拉出了一些其他的......我会让你过滤它们:-)

