本文介绍了ASP.NET 5/MVC 6 Ajax 将模型发布到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
在我的 ASP.NET 5 MVC 6 应用程序中,我想使用 Ajax 将一些数据发布到我的控制器.我已经用 ASP.NET MVC 5 完成了这项工作,并在一个空白的 ASP.NET MVC 5 项目中测试了完全相同的代码并且它可以工作,但是对于新版本我不能,我不知道为什么.通过 Ajax 调用,我可以转到控制器,创建模型但字段为空(或布尔值为假).这是我的代码:
In my ASP.NET 5 MVC 6 application, I want to post with Ajax some data to my controller. I already done this with ASP.NET MVC 5 and I tested the exact same code in an blank ASP.NET MVC 5 project and it worked, but with the new version I can't and I don't know why. With the Ajax call, I can go to the controller, the model is created but the fields are null (or false for the boolean). Here is my code :
script.js:
var data = {
model: {
UserName: 'Test',
Password: 'Test',
RememberMe: true
}
};
$.ajax({
type: "POST",
url: "/Account/Login/",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do something interesting here.
}
});
AccountController.cs:
AccountController.cs :
[HttpPost]
public JsonResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
//var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
//if (result.Succeeded)
//{
// //return RedirectToLocal(returnUrl);
/
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!