嘿我试图将表单作为Json发布到我的控制器,然后更新列匹配的数据库。我似乎无法让它发挥作用。我正在使用如下所示的函数将表单数据转换为json字符串并且它正在发布但我遇到了控制器方法的问题,我需要找出如何将json中的valuse与数据库中的值进行匹配任何帮助都会很棒
$.ajax({
type: 'POST',
contentType: 'application/json',
url: "Admin/update/",
dataType: "json",
data: formToJSON(),
success: function (themessage) {
}
});
使用JSON.stringify转换表单
function formToJSON() {
return JSON.stringify({
"username": $('#username').val(),
"fname": $('#fname').val(),
"lname": $('#lname').val(),
"address1": $('#address1').val(),
"address2": $('#address2').val(),
"postcode": $('#postcode').val(),
"town": $('#town').val(),
"memcat": $('#memcat').val(),
"phone": $('#phone').val(),
"email": $('#email').val(),
"message": $('#message').val(),
"password": $('#password').val(),
"Admin": $('#Admin').val(),
});
}
发布包含
{"username":"Broon","fname":"Paw","lname":"Broon","address1":"12 Glebe Street","address2":"Sinderins","postcode":"DD12 4RT","town":"Dundee","memcat":"Ordinary","phone":"123456","email":"paw@localhost","password":"paw","Admin":"no"}
控制器方法 // // POST:/ Admin /
public JsonResult update(string username)
{
var themessage = from b in db.member
select b;
themessage = themessage.Where(b => username.Contains(b.username));
foreach (var item in themessage)
{
item.username = username;
}
db.SaveChanges();
return Json(themessage, JsonRequestBehavior.AllowGet);
}
问题是访问json数据以匹配数据库数据。我猜我可能需要以某种方式解析json?上面我只试图匹配用户名。