jQuery 将对象从 JSON 传递到 MVC 控制器 - 它始终为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4656232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:39:01  来源:igfitidea点击:

Pass object from JSON into MVC Controller - its always null?

jqueryasp.net-mvc-2

提问by SteveCl

I have seen a few questions on here related to the a similar issue, I have read them, followed them, but still i have the same problem.

我在这里看到了一些与类似问题相关的问题,我已经阅读并关注了它们,但我仍然遇到同样的问题。

I am basically creating an object in javascript and trying to call a method on the controller that will return a string of html. Not JSON.

我基本上是在 javascript 中创建一个对象,并尝试在控制器上调用一个方法,该方法将返回一串 html。不是 JSON。

I've been playing around with dataType and contentType but still no joy. So apologies if the code snippets are a bit messy.

我一直在玩 dataType 和 contentType 但仍然没有乐趣。如果代码片段有点乱,请见谅。

Build the object in JS.

在 JS 中构建对象。

function GetCardModel() {
    var card = {};
    card.CardTitle = $("#CardTitle").val();
    card.TopicTitle = $("#TopicTitle").val();
    card.TopicBody = $("#TopicBody").data("tEditor").value();
    card.CardClose = $("#CardClose").val();
    card.CardFromName = $("#CardFromName").val();
    return card;
}

Take a look at the object - all looks good and as it should in JSON.

看一看对象 - 一切看起来都不错,并且在 JSON 中应该如此。

var model = GetCardModel();
alert(JSON.stringify(GetCardModel()));

Make the call...

打电话...

$.ajax({
            type: "POST",
            url: "/Postcard/Create/Preview/",
            dataType: "json",
            //contentType: "application/json",
            data: GetCardModel(),
            processData: true,
            success: function (data) {
                alert("im back");
                alert(data);
            },
            error: function (xhr, ajaxOptions, error) {
                alert(xhr.status);
                alert("Error: " + xhr.responseText);
                //alert(error);
            }
        });

Always when I step into the controller, the object is ALWAYS there, but with null values for all the properties.

总是当我进入控制器时,对象总是在那里,但所有属性的值都是空值。

回答by Darin Dimitrov

The parameter name should be data, not date:

参数名称应该是data,而不是date

$.ajax({
    type: 'POST',
    url: '/Postcard/Create/Preview/',
    dataType: 'json',
    data: {
        CardTitle: $("#CardTitle").val(),
        TopicTitle: $("#TopicTitle").val(),
        TopicBody: $("#TopicBody").data("tEditor").value(),
        CardClose: $("#CardClose").val(),
        CardFromName: $("#CardFromName").val()
    },
    success: function (data) {
        alert('im back');
        alert(data);
    },
    error: function (xhr, ajaxOptions, error) {
        alert(xhr.status);
        alert('Error: ' + xhr.responseText);
    }
});

which will successfully call the following controller action and the action parameter will be properly bound:

这将成功调用以下控制器动作,并且动作参数将被正确绑定:

[HttpPost]
public ActionResult Preview(Card card) { ... }

with the model below:

使用以下模型:

public class Card
{
    public string CardTitle { get; set; }
    public string TopicTitle { get; set; }
    public string TopicBody { get; set; }
    public string CardClose { get; set; }
    public string CardFromName { get; set; }
}