ASP。Net MVC自定义模型绑定说明

时间:2022-11-30 16:32:13

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: here and here. The first is I believe out of date and the second doesn't really explain what is going on.

我已经发现了一些使用默认模型绑定器的问题(在创建新对象时不填充,在编辑对象上创建副本),并发现我的UI与模型完全不匹配(例如,时间字段格式为“hh:mm am”与timespan不匹配)。因此,我想看看创建自定义模型绑定器是否是更好的解决方案。我找到了几个例子:这里和这里。第一个是我相信已经过时了,第二个并没有真正解释发生了什么。

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实现的关系,我会很感激。

2 个解决方案

#1


30  

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

#2


4  

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

这里有一个关于模型绑定的很好的解释,我相信它将会涉及到您的问题:http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx。

A quick Google search pulled up a few others... I will let you filter through them :-)

谷歌快速搜索了一些其他的……我会让你过滤它们:

#1


30  

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

#2


4  

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

这里有一个关于模型绑定的很好的解释,我相信它将会涉及到您的问题:http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx。

A quick Google search pulled up a few others... I will let you filter through them :-)

谷歌快速搜索了一些其他的……我会让你过滤它们: