ASP.NET MVC2 - 自定义模型绑定器示例

发布于 2024-08-23 20:30:57 字数 380 浏览 8 评论 0原文

我试图找到一些为我需要处理的独特绑定场景构建自定义模型绑定程序的示例,但我找到的所有文章都是针对旧版本的 MVC,这些版本在 MVC2 中不再相关。我一直在参考 DefaultModelBinder 源代码,试图对我需要做的事情有一个总体的感觉,但它比我的场景要复杂得多,而且我在隔离我需要实现的特定逻辑时遇到了困难。

我的目标是获取复选框/文本框对的集合,对于所有选中的对,我想创建复选框值和关联文本框值的键/值对。聚合这些数据后,我需要对集​​合进行一些字符串序列化,以便可以将其存储在所需模型类型的字符串属性中。我已经以可管理的格式从表单发送数据,这将允许我将给定的复选框与特定的文本框相关联,这只是弄清楚如何将所有部分获取到我需要的地方的问题。

有谁知道一些可以帮助我开始构建自定义模型绑定程序的最新教程?

I am trying to find some examples of building a custom model binder for a unique binding scenario I need to handle, but all of the articles I found were for older versions of MVC which are no longer relevant in MVC2. I've been referencing the DefaultModelBinder source code to try to get a general feel for what I need to do, but it's entirely more complicated than my scenario and I'm having trouble isolating the specific logic I need to implement.

My goal is to take a collection of Checkbox/Textbox pairs and for all of the Checked pairs I would like to create a key/value pair of the Checkbox's value and the associated Textbox's value. After aggregating this data I need to do some string serialization on the collection so I can store it in a string property of the desired Model type. I already the data being sent from the form in a manageable format which will allow me to relate a given Checkbox to a specific Textbox, it's just a matter of figuring out how to get all the pieces where I need them.

Does anyone know of some up-to-date tutorials that can get me started with building a custom model binder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后eg是否自 2024-08-30 20:30:57

我不知道为什么您认为自 MVC 1 以来关于自定义模型绑定器发生了很多变化。但如果我明白你想要做什么,那应该相当容易。

public class CustomModelBinder : DefaultModelBinder {
    public override object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext) {

        NameValueCollection form = controllerContext.HttpContext.Request.Form;
        //get what you need from the form collection

        //creata your model
        SomeModel myModel = new SomeMode();
        myModel.Property = "value";
        //or add some model errors if you need to
        ModelStateDictionary mState = bindingContext.ModelState;
        mState.Add("Property", new ModelState { });
        mState.AddModelError("Property", "There's an error.");

        return myModel; //return your model
    }
}

您的行动:

public ActionResult Contact([ModelBinder(typeof(CustomModelBinder))]SomeModel m){
    //...
}

这是您正在寻找的信息吗?

I don't know why you think a lot has changed since MVC 1 regarding custom model binders. But If I understand what you are trying to do, it should be fairly easy.

public class CustomModelBinder : DefaultModelBinder {
    public override object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext) {

        NameValueCollection form = controllerContext.HttpContext.Request.Form;
        //get what you need from the form collection

        //creata your model
        SomeModel myModel = new SomeMode();
        myModel.Property = "value";
        //or add some model errors if you need to
        ModelStateDictionary mState = bindingContext.ModelState;
        mState.Add("Property", new ModelState { });
        mState.AddModelError("Property", "There's an error.");

        return myModel; //return your model
    }
}

And your action :

public ActionResult Contact([ModelBinder(typeof(CustomModelBinder))]SomeModel m){
    //...
}

Was that the kind of information you are looking for?

天气好吗我好吗 2024-08-30 20:30:57

Take a look at several examples of Custom MVC Model binders on my blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文