简体   繁体   English

MVC模型绑定JSON

[英]MVC Model binding JSON

I have a JS file that creates a JSON string that I stringify and post with AJAX call to the server, using Fiddler I can see the JSON is formatted correctly and my Action is called as it stops at the breakpoint but my Model has just Nulls. 我有一个JS文件,该文件创建了一个JSON字符串,该字符串进行了字符串化并通过AJAX调用发布到服务器,使用Fiddler,我可以看到JSON的格式正确,并且因为它在断点处停止而调用了我的Action,但是我的模型只有Nulls。

Here is the JS code: 这是JS代码:

    var testString = { id: "1", date: "28/04/2013", sim: "B787", times: "0100", note: "Test note" };

    $.ajax({
        url: url,
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(testString),
        success: function (data) {
        },
        error: function (request, status, error) {
        }
    });

And here is the Model: 这是模型:

[Serializable]
  public class BookingModel
  {
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public string Sim { get; set; }
    public string Times { get; set; }
    public string Note { get; set; }
  }

Finally here is the Action that is called. 最后是被调用的动作。

    [HttpPost]
    //[ValidateAntiForgeryToken]
    public ActionResult Booking(BookingModel BookingModel)
    {

        return Json(new { sucess = 0});
    }

And during debug the BookingModel shows nothing but NULL's but as I mention abouve FIddler is showing the string as: 并且在调试过程中,BookingModel仅显示NULL,但正如我所说,FIddler将该字符串显示为:

{"id":"1","date":"28/04/2013","sim":"B787","times":"0100","note":"Test note"}

I have been at this for 2 days now and I'm getting bald someone please help... It's driving me nuts. 我已经在这里待了两天了,我要秃头的人请帮忙...这让我发疯了。

Thanks. 谢谢。

Cliff. 悬崖。

EDIT * 编辑 *

Ok bit more info looks like it does work but what I have to do is on the first breakpoint if I hit F5 to continue to the action is called again and this time the data is present and correct. 好的,更多信息看起来确实有效,但是我必须做的是在第一个断点上,如果我按F5键继续操作,则再次调用该命令,这次数据存在且正确。

So my question now is as it works on the second round trip to the server whay not on the first? 所以我现在的问题是,它可以在第二次往返服务器的旅程中工作,而不是第一次往返吗?

Thanks 谢谢

Cliff. 悬崖。

Just happened to stumble across this one while I was looking for something else. 当我在寻找其他东西时,碰巧偶然碰到了这个。

I encountered a similar thing when I first started using MVC, and it was case sensitivity that was the issue. 刚开始使用MVC时,我遇到了类似的问题,这是区分大小写的问题。

In your JSON Object all your property names are lower case, in your actual C# model, your property names are capitalized. 在JSON对象中,所有属性名称均为小写,在实际的C#模型中,属性名称为大写。

I'm going to make the assumption that a year later you've already figured this one out, but in the spirit of providing an answer for future searchers. 我将假设一年后您已经弄清楚了这一点,但本着为将来的搜索者提供答案的精神。

Try making sure that the field names in your JSON object match exactly the casing of the property names in your backing model. 尝试确保JSON对象中的字段名称与后备模型中属性名称的大小写完全匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM