我正在尝试使用jquery:
从MVC4视图将JSON发布到控制器var dataToSend = $('#mainForm').serialize();
//if ($(this).valid()) {
$.ajax({
url: "/Account/Registration",
type: "POST",
data: dataToSend,
success: function (result) {
afterSubmit(result);
}
});
我可以看到该模型在服务器上有数据,我正在寻找一种方法将模型直接绑定到EF实体。 我想我可以迭代所有模型的属性并添加到新注册实体,但想看看是否有人可以提出任何建议。我在db.SaveChanges();
上收到错误[HttpPost]
[AllowAnonymous]
public JsonResult Registration(Registration model)
{
if (ModelState.IsValid)
{
try
{ //save to db
using (myEntities db = new myEntities())
{
Registration r = new Registration();
db.Registrations.Add(model);
db.SaveChanges();
}
return Json(new {Success = true, Message = "Registered"});
}
catch (Exception)
{
return Json(new {Success = false, Message = "Save Errors"});
throw;
}
}
else
{
return Json(new {Success = false, Message = "Validation Errors"});
}
}