ASPNET MVC - 为什么ModelState.IsValid假" X字段是必需的"当该字段具有值? [英] ASPNET MVC - Why is ModelState.IsValid false "The x field is required" when that field does have a value?

查看:1017
本文介绍了ASPNET MVC - 为什么ModelState.IsValid假" X字段是必需的"当该字段具有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个模型:

public PurchaseOrder 
{
    [Required] [StringLength(15)]
    public virtual string OrderNumber {get;set;}
    // etc.        
}

当我从视图中提交订单(使用$。员额,不输入类型=提交),它进入我的控制器类:

When I submit an order from the view (using $.post, not input type=submit) it goes to my controller class:

public class PurchaseOrderController
{
    public JsonResult Save(PurchaseOrder order)
    {
        // TryUpdateModel(order); // commented out since modelstate.isvalid remains false anyway
        if (ModelState.IsValid)
        {
            // its never valid 
        }
    }
}

ModelState.IsValid总是返回false,出现错误:是必需的订单号场。但是有一个值,在这个领域(为什么?)

ModelState.IsValid always returns false, with the error: "The Order Number field is required." But there is a value in this field (?? why)

为什么它会说:值是必需的,当它的确具有价值?我错过了什么?是不是因为.post的$,而不是提交?我该怎么办?

Why would it say "value is required" when it does have a value? Have I missed something? Is it because of the $.post instead of the submit? What can I do?

这是调试器是什么样子:

This is what the debugger looks like:

编辑:额外的信息....

我真的认为,由于某种原因,模型绑定没有发生。当我尝试这个code在这里找到:)

I really think that for some reason the model binding is not happening. When I try this code found here: )

if (!ModelState.IsValid)
{
    ModelState.Clear();
    ModelMetadata modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => order, order.GetType());
    ModelValidator compositeValidator = ModelValidator.GetModelValidator(modelMetadata, base.ControllerContext);

    foreach (ModelValidationResult result in compositeValidator.Validate(null))
    {
        this.ModelState.AddModelError(result.MemberName, result.Message);
    }
}

然后ModelState.IsValid = TRUE。 compositeValidator.Validate()返回0错误。我认为这表明该模型是不绑定的,但我仍然不知道为什么。

Then ModelState.IsValid = true. compositeValidator.Validate() returns 0 errors. I think this indicates the model was not bound, but I still don't know why.

控制器方法实际上看起来像这样(我错过了过滤器时,最初写这个问题)

The controller method actually looks like this (I missed out the filter when originally writing this question)

[JsonFilter(Param = "order", JsonDataType = typeof(PurchaseOrder))] 
public JsonResult Save(PurchaseOrder order) { //  etc ... }

而JsonFilter并从JSON数据提交此提取POCO:

And the JsonFilter does this to extract the POCO from the json submitted data:

filterContext.ActionParameters[Param] 
    = jsSerializer.Deserialize(inputContent, JsonDataType);

我把一个断点在这条线上,和秩序是有效的,再加上order.OrderNumber有正确的值。

I put a breakpoint on this line, and order is valid, plus order.OrderNumber has the correct value.

所以仍然没有得到解决,但希望这额外的信息将会找到一个解决方案帮助

So still unresolved, but hopefully this extra info will help with finding a solution

推荐答案

好吧,我已经解决了,但我真的不明白,为什么我的变化作出都有帮助。

Well I have "solved" it, but I do not really understand why the changes I made have helped.

我必须做三件事:


  1. 删除JSON过滤器(过滤器不绑定)

  1. Remove the json filter (filters don't bind)

更改的contentType为application / JSON

Change the contentType to application/json

$。ajaxSetup({
  的contentType:应用/ JSON的;字符集= UTF-8
});

$.ajaxSetup({ contentType: "application/json; charset=utf-8" });

使用的MVC期货作为这里描述下载Microsoft.Mvc.dll:<一href=\"http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx\" rel=\"nofollow\">http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx.哪里是说把它添加到的Application_Start()在Global.asax.cs中:

Use the MVC futures download Microsoft.Mvc.dll as described here: http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx. Where is says to add this to Application_Start() in Global.asax.cs:

ValueProviderFactories.Factories.Add(新JsonValueProviderFactory());

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

现在我不知道为什么已经工作,但它有。

Now I dont know exactly why that has worked, but it has.

不幸的是,已经有一个负面效应:contentType为适用于所有$。获得()和$。员额()方法,并打破了我的所有jqgrids - 他们似乎只是工作,如果内容类型是默认应用程序/ x-WWW的形式urlen codeD

Unfortunately it has had a negative side effect: the contenttype is applied to all $.get() and $.post() methods, and broken all my jqgrids - they only seem to work if the content type is the default of application/x-www-form-urlencoded

于是我问2后续的问题:

So I've asked 2 follow on questions:


  1. 是否有可能设置在.post的$内容类型()调用?然后,我就不需要全局设置它
    <一href=\"http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttypeapplication-json\">http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttypeapplication-json

时,有可能使jqrid的工作,如果contentType为application / JSON?
<一href=\"http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttypeapplication-json\">http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttypeapplication-json

Is it possible to make jqrid work if the contenttype is application/json? http://stackoverflow.com/questions/2845459/jquery-how-to-make-post-use-contenttypeapplication-json

这篇关于ASPNET MVC - 为什么ModelState.IsValid假&QUOT; X字段是必需的&QUOT;当该字段具有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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