(转)How to use the ASP.NET MVC ModelBinder
How to use the ASP.NET MVC ModelBinder
http://weblogs.asp.net/melvynharbour/archive/2008/08/29/how-to-use-the-asp-net-mvc-modelbinder.aspx
One of the new features in the latest build of ASP.NET MVC is the ModelBinder, which is provided to allow Action methods to take complex types as their parameters. Previously, action methods were only able to take simple types such as strings and integers as their parameters. The new ModelBinder provides the facility to build complex types from component parts that (for example) may be part the result of submitting a form with several fields. To see an example of this in action, we will first need a fairly simple class to work with. The following class definition will typically be placed in the Models directory. I have left the using statements at the start of the file as an 'exercise for the reader'!
1: public class MBTest
2: {
3: public string Name { get; set; }
4:
5: public MBTest()
6: {
7: }
8: }
All fairly standard stuff. Now let's create a form on our index page that we can use to have a play with one of these objects:
1: <% using (Html.Form("Home", "About")) { %>
2: <input type="text" name="Name" />
3: <button type="submit">Submit</button>
4: <% } %>
Again, just the relevant form supplied here, and kept as simple as possible for clarity. Nothing in the routing needs to change: that will all happen just fine. Now, the aim is to write an Action Method that looks like the following:
1: public ActionResult About(MBTest testItem)
2: {
3: ViewData["Title"] = "About Page";
4:
5: return View();
6: }
Admittedly, this method doesn't actually do anything with the testItem object that it creates, but it could quite easily if it wanted to. What we want is that the Name property of testItem is populated with the contents of the text input field in our form. Previously, we would have done this by inserting code into the About method above to parse the results coming back. So here's the new bit. We create a helper class that will carry the knowledge of how to perform the translation from the form to the complex type. This class must implement the new interface IModelBinder.
public class MBTestBinder : IModelBinder
2: {
3: #region IModelBinder Members
4:
5: public object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState)
6: {
7: MBTest instance = new MBTest();
8: instance.Name = controllerContext.HttpContext.Request["Name"];
9: return instance;
10: }
11:
12: #endregion
13: }
Obviously this is a very simple example, and I haven't used most of the information passed to the GetValue method, but the intention of this post is primarily to be a starting point, just to get you 'up and running'. So the MBTestBinder class functions as a bridge, in a similar way to an ADO.NET DataAdapter. There's one final step to getting this all hooked up, and that's to tell the code to use MBTestBinder. This is done by applying the new ModelBinderAttribute to things. There are actually two different places you can use this attribute, with the same effect. The first is by decorating the model class:
1: [ModelBinder(typeof(MBTestBinder))]
2: public class MBTest
The second is decorating the parameter of your Action Method directly:
1: public ActionResult About([ModelBinder(typeof(MBTestBinder))]MBTest testItem)
My guess is that they've left the choice in because the decision about exactly where bridge classes such as MBTestBinder actually lives is not a straightforward one. Others will no doubt contribute much more involved examples and discuss whether this solution to the problem is even the correct one, but hopefully this post explains enough to enable people to get started with experimenting with it and getting a feel for it.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?