如何获得对模型绑定的控制? [英] How to gain control over model binding?

查看:70
本文介绍了如何获得对模型绑定的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用MVC和我越来越有点失望。而不是帮助我,框架越来越我的路。

I started using MVC recently and I'm getting little disappointed. Instead of helping me, the framework is getting in my way.

我试图写这样的(伪code)的控制器操作

I'm trying to write a controller action like this (pseudo code)

ActionResult Save(long id, string whichForm)
{
    if (whichForm == "A")
    {
        var vm = CreateModel(Request.Form);
        if (!TryValidate(vm))
            return View(vm);
        else
            return RedirectToRoute("Success");
    }
    else ....
}

基本上我想有,当我的视图模型,并当它验证了控制。这可能吗?我怎样才能实现CreateModel的方法?想想我可能要到该控制器操作中创建多个不同的视图模式。

Basically I'd like to have control over when my view-model is constructed and when it is validated. Is this possible? How can I implement CreateModel method? Consider I may want to create several different view-models within this controller action.

*咆哮:我真的不明白,为什么视图模型绑定和验证在DefaultModelBinder混合在一起。好像code气味。特别是当你很难覆盖这一行为。

*Rant: I don't really understand why view-model binding and validation are mixed together in DefaultModelBinder. Seems like code smell. Specially when it's hard to override this behaviour.

推荐答案

您可以创建和你的自由裁量权绑定到现有的模型:

You can create and bind to an existing model at your discretion:

public ActionResult Save(long id, string whichForm)
{
    if (whichForm == "A")
    {
        var vm = new FormAViewModel();

        if (!TryUpdateModel(vm))
            return View(vm);
        else
            return RedirectToRoute("Success");
    }
    // else ....
}

您还可以创建自己的选择 IModelBinder ,如果​​你想在绑定过程的完全控制。您可以替换默认的模型绑定,也可以具体 IModelBinder 实现特定类型的注册。我建议,但是,除非你的绑定的逻辑很简单,你可能会想从 DefaultModelBinder 派生自定义模型绑定,只是覆盖的部分你不喜欢。

You also have the option of creating your own IModelBinder, if you want complete control over the binding process. You can replace the default model binder, or you can register specific IModelBinder implementations for specific types. I would suggest, however, that unless your binding logic is simple, you will probably want to derive your custom model binder from DefaultModelBinder and just override the parts you don't like.

我不想离开一个巨魔十岁上下的评论,但9次了10人的原因,感觉一个框架,他们的方式也越来越是因为他们还不知道如何正确使用它。 <一href=\"http://odeto$c$c.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx\">Here是与模型绑定一般提示的文章。

I hate to leave a troll-ish comment, but 9 times out of 10 the reason someone feels a framework is getting in their way is because they don't yet understand how to properly use it. Here is an article with general tips on model binding.

至于你咆哮:验证并绑定是分开的,但是默认的模型绑定不触发验证。这样做的原因是为了让您的应用程序优雅地处理问题,结合缺少/无效/不完整的价值观,而不是让结合静默失败或抛出异常。

As to your rant: Validation and Binding are separate, however, the default model binder does trigger validation. The reason for this is to allow your application to gracefully handle problems binding to missing/invalid/incomplete values, rather than allowing binding to fail silently or throwing exceptions.

这篇关于如何获得对模型绑定的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆