通过视图模型有两个对象使用JSON到控制器 [英] Pass viewmodel with two objects to controller using Json
本文介绍了通过视图模型有两个对象使用JSON到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的code。下面的JSON是不正确的,但我想我靠近。控制器越来越空数据每次。任何帮助将是AP preciated。
$(#btnRegister)。点击(函数(){
VAR personModel = {
姓:$(#txtFirstName)VAL()
名字:$(#txtLastName)VAL()
电话:$(#txtPhone)VAL()。
EmailAddress的:$(#txtEmail)VAL()。
};
VAR loginModel = {
用户名:$(#txtUserName)VAL()
密码:$(#txtPassword)VAL()。
};
VAR registerViewModel = {
WEBUSER:loginModel,
联系人:personModel
};
$阿贾克斯({
网址:@ Url.Action(注册,用户),
键入:POST,
数据:registerViewModel,
成功:函数(结果){
警报(结果);
}
});
});
[HttpPost]
公共JsonResult寄存器(RegisterViewModel registerViewModel)
{
字符串名称= registerViewModel.Person.FirstName;
字符串的用户名= registerViewModel.WebUser.UserName;
返回JSON(名);
}
公共类RegisterViewModel
{
公共WEBUSER WEBUSER {获得;组; }
公众人物的人{获得;组; }
}
公共类WEBUSER
{
[键]
公共字符串用户ID {获得;组; }
公共字符串用户名{获得;组; }
公共字符串密码{获得;组; }
}
公共类Person
{
[键]
公众诠释PERSONID {获得;组; }
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
公共字符串电话{获得;组; }
公共字符串EmailAddress的{获得;组; }
公共字符串EmailConfirmation {获得;组; }
公开日期时间dateCreated会{获得;组; }
公共字符串用户ID {获得;组; }
}
解决方案
AJAX的部分需要是这样
$。阿贾克斯({
网址:@ Url.Action(注册,用户),
键入:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
数据:JSON.stringify(registerViewModel)
成功:函数(结果){
警报(结果);
}
});
希望这有助于!
Here is my code. The JSON below is not correct but I think I am close. The controller is getting null data everytime. Any help would be appreciated.
$( "#btnRegister" ).click(function() {
var personModel = {
FirstName: $("#txtFirstName").val(),
LastName: $("#txtLastName").val(),
Phone: $("#txtPhone").val(),
EmailAddress: $("#txtEmail").val()
};
var loginModel = {
UserName: $("#txtUserName").val(),
Password: $("#txtPassword").val()
};
var registerViewModel = {
WebUser: loginModel,
Person: personModel
};
$.ajax({
url: "@Url.Action("Register", "User")",
type: 'POST',
data: registerViewModel,
success: function(result) {
alert(result);
}
});
});
[HttpPost]
public JsonResult Register(RegisterViewModel registerViewModel)
{
string name = registerViewModel.Person.FirstName;
string username = registerViewModel.WebUser.UserName;
return Json(name);
}
public class RegisterViewModel
{
public WebUser WebUser { get; set; }
public Person Person { get; set; }
}
public class WebUser
{
[Key]
public string UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class Person
{
[Key]
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string EmailAddress { get; set; }
public string EmailConfirmation { get; set; }
public DateTime DateCreated { get; set; }
public string UserId { get; set; }
}
解决方案
The ajax part needs to be like this
$.ajax({
url: "@Url.Action("Register", "User")",
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(registerViewModel),
success: function(result) {
alert(result);
}
});
Hope this helps!
这篇关于通过视图模型有两个对象使用JSON到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文