我有一个复杂的JSON对象,它被发送到View没有任何问题(如下所示)但我无法弄清楚如何通过AJAX调用将这些数据序列化回.NET对象.各部分的细节如下.
var ObjectA = {
"Name": 1,
"Starting": new Date(1221644506800),
"Timeline": [
{
"StartTime": new Date(1221644506800),
"GoesFor": 200
}
,
{
"StartTime": new Date(1221644506800),
"GoesFor": 100
}
]
};
Run Code Online (Sandbox Code Playgroud)
我不确定如何将此对象传递给Controller方法,我在下面使用此方法,其中Timelines对象使用Properties镜像上述JS对象.
public JsonResult Save(Timelines person)
Run Code Online (Sandbox Code Playgroud)
我使用的jQuery是:
var encoded = $.toJSON(SessionSchedule);
$.ajax({
url: "/Timeline/Save",
type: "POST",
dataType: 'json',
data: encoded,
contentType: "application/json; charset=utf-8",
beforeSend: function() { $("#saveStatus").html("Saving").show(); },
success: function(result) {
alert(result.Result);
$("#saveStatus").html(result.Result).show();
}
});
Run Code Online (Sandbox Code Playgroud)
我已经看到这个问题类似,但不完全相同,因为我没有使用表单来操纵数据. 如何使用json将复杂类型传递给ASP.NET MVC控制器
我也看到过使用'JsonFilter'手动反序列化JSON的引用,但是想知道是否有办法通过ASP.NET MVC进行本地化的操作?或者以这种方式传递数据的最佳做法是什么?
我一直在看这里的问题:MVC ajax json发布到控制器动作方法但不幸的是它似乎没有帮助我.除了我的方法签名之外,我的几乎完全相同(但我已经尝试过了,但它仍然没有被击中).
$('#loginBtn').click(function(e) {
e.preventDefault();
// TODO: Validate input
var data = {
username: $('#username').val().trim(),
password: $('#password').val()
};
$.ajax({
type: "POST",
url: "http://localhost:50061/checkin/app/login",
content: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
success: function(d) {
if (d.success == true)
window.location = "index.html";
else {}
},
error: function (xhr, textStatus, errorThrown) {
// TODO: Show error
}
});
});
Run Code Online (Sandbox Code Playgroud)
[HttpPost]
[AllowAnonymous]
public JsonResult Login(string username, string password)
{
string error = "";
if (!WebSecurity.IsAccountLockedOut(username, 3, 60 * 60)) …Run Code Online (Sandbox Code Playgroud)