将对象从 JSON 传递到 MVC 控制器 - 它始终为 null?
我在这里看到了一些与类似问题相关的问题,我已阅读它们并关注它们,但我仍然遇到同样的问题。
我基本上是在 javascript 中创建一个对象,并尝试调用控制器上的一个方法,该方法将返回一个 html 字符串。不是 JSON。
我一直在玩弄 dataType 和 contentType 但仍然没有乐趣。如果代码片段有点混乱,我们深表歉意。
在 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;
}
看一下该对象 - 一切看起来都很好,并且在 JSON 中应该如此。
var model = GetCardModel();
alert(JSON.stringify(GetCardModel()));
进行调用...
$.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);
}
});
总是当我进入控制器时,该对象始终存在,但所有属性都为空值。
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.
I've been playing around with dataType and contentType but still no joy. So apologies if the code snippets are a bit messy.
Build the object in 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参数名称应该是
data
,而不是date
:这将成功调用以下控制器操作,并且操作参数将正确绑定:
与以下模型:
The parameter name should be
data
, notdate
:which will successfully call the following controller action and the action parameter will be properly bound:
with the model below: