在自定义模型活页夹的ModelState值 [英] Setting ModelState values in custom model binder

查看:112
本文介绍了在自定义模型活页夹的ModelState值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的自定义模型粘合剂在ASP.NET MVC 2,看起来像这样:

I am using custom model binder in ASP.NET MVC 2 that looks like this:

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }

        BaseContentObject obj = (BaseContentObject)base.BindModel(controllerContext, bindingContext);
        if(string.IsNullOrWhiteSpace(obj.Slug))
        {
            // creating new object
            obj.Created = obj.Modified = DateTime.Now;
            obj.ModifiedBy = obj.CreatedBy = controllerContext.HttpContext.User.Identity.Name;
            // slug is not provided thru UI, derivate it from Title; property setter removes chars that are not allowed
            obj.Slug = obj.Title;
            ModelStateDictionary modelStateDictionary = bindingContext.ModelState;
            modelStateDictionary.SetModelValue("Slug", new ValueProviderResult(obj.Slug, obj.Slug, null));
...

当我从这个粘结剂回控制器动作,那就是作为参数提供给我的行动业务对象正确改变(行obj.Created = ....工作)。

When I get back from this binder into controller action, my business object that is provided as a parameter to the action is correctly altered (the lines obj.Created = .... work).

然而,ModelState中不更新。我知道这是因为我需要对我的业务对象的弹头财产,虽然我在自定义模型粘结剂改变ModelStateDictionary,提供了一个弹头给它(你可以在上面看到),该ModelState.IsValid还是假的。

However, the ModelState is not updated. I know this because I have Required on my business object's Slug property and although I altered ModelStateDictionary in my custom model binder, providing a Slug to it (as you can see above), the ModelState.IsValid is still false.

如果我把ModelState中[弹头]在调试会话我的观察窗口,它说,它有错误(1),所以显然它是空的,因此失败。

If I put ModelState["Slug"] in my Watch window in Debug session, it says it has Errors (1), so apparently it is empty and as such fails.

我怎样才能正确地改变ModelState中的自定义模型粘合剂code里面呢?

How can I correctly alter the ModelState inside the custom model binder code?

推荐答案

显然是没有办法重新验证的ModelState一旦你改变一些关键的值。和isValid仍然false,因为一个新的值设置为一些关键不触发重新验证。

Apparently there is no way to revalidate the ModelState once you change a value of some key. The IsValid remains false because setting a new value to some key does not trigger revalidation.

解决方案是先删除触发IsValid的是假的,并重新创建它和值分配给它的关键。当你做到这一点的ModelState自动重新验证,如果一切都很好,的IsValid返回true。

The solution is to first remove the key that triggered IsValid to be false and recreate it and assign the value to it. When you do that the ModelState automatically revalidates and if everything is fine, IsValid returns true.

这样的:

bindingContext.ModelState.Remove("Slug");
bindingContext.ModelState.Add("Slug", new ModelState());
bindingContext.ModelState.SetModelValue("Slug", new ValueProviderResult(obj.Slug, obj.Slug, null));

这篇关于在自定义模型活页夹的ModelState值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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