使用 jquery ajax 序列化的模型绑定不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16717715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Model binding with jquery ajax serialize not working
提问by mosquito87
I have the following model:
我有以下模型:
public class RegisterUseraccount
{
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "E-Mail-Adresse")]
public string Email { get; set; }
[Required]
[Display(Name = "Vorname")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Nachname")]
public string LastName { get; set; }
[Required]
[DataType(DataType.Password)]
[MinLength(5)]
[Display(Name = "Passwort")]
public string Password { get; set; }
...
}
And the following view:
以及以下视图:
@using (Html.BeginForm("Register", "Useraccount", FormMethod.Post, new { id = "registerUseraccountForm", @class = "ym-form" }))
{
@Html.ValidationSummary(true)
<div class="ym-grid">
<div class="ym-g50 ym-gl">
<div class="ym-fbox-text">
@Html.LabelForRequired(model => model.RegisterUseraccount.FirstName, null)
@Html.EditorFor(model => model.RegisterUseraccount.FirstName, new { required = "required", name = "firstName" })
@Html.ValidationMessageFor(model => model.RegisterUseraccount.FirstName)
</div>
</div>
...
and my JavaScript
和我的 JavaScript
function sendForm(target) {
alert(data);
$.ajax({
url: target,
type: "POST",
contentType: 'application/json',
data: $("#registerUseraccountForm").serialize(),
success: ajaxOnSuccess,
error: function (jqXHR, exception) {
alert('Error message.');
}
});
This is the result of the serialization:
这是序列化的结果:
RegisterUseraccount.FirstName=Peter&RegisterUseraccount.LastName=Miller&RegisterUseraccount.Email=miller%40gmail.com&RegisterUseraccount.Password=admin
This is my controller method I'm trying to POST to:
这是我试图发布到的控制器方法:
[HttpPost]
public ActionResult Register(RegisterUseraccount registerUseraccount)
{
...
}
... but the data doesn't arrive at the method, I get an error 404. I think the modelbinder can't work.
...但数据没有到达方法,我收到错误 404。我认为模型绑定器无法工作。
What works is data which is sent with the name firstName=Peter, but what actually is sent is RegisterUseraccount.FirstName=Peter.
有效的是以名称 firstName=Peter 发送的数据,但实际发送的是 RegisterUseraccount.FirstName=Peter。
How can I handle this problem?
我该如何处理这个问题?
回答by AliR?za Ad?yah?i
remove contentType: 'application/json',
and modify it to better (from my perspective)
删除contentType: 'application/json',
并修改它以更好(从我的角度来看)
$('#registerUseraccountForm').submit(function () {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
beforeSend: function () {
},
complete: function () {
},
...
回答by andres descalzo
Maybe you have this model
也许你有这个模型
public class YourModel
{
public RegisterUseraccount RegisterUseraccount { get; set; }
}
In this case you have to put the model that corresponds to your action:
在这种情况下,您必须放置与您的操作相对应的模型:
[HttpPost]
public ActionResult Register(YourModel model)
{
var registerUseraccount = model.RegisterUseraccount;
...
}
Or:
或者:
@using (Html.BeginForm("Register", "Useraccount", FormMethod.Post, new { id = "registerUseraccountForm", @class = "ym-form" }))
{
@{ Html.RenderPartial("RegisterUseraccount"); }
}
RegisterUseraccount.cshtml
注册用户账号.cshtml
@model YourNamespace.RegisterUseraccount
@Html.ValidationSummary(true)
<div class="ym-grid">
<div class="ym-g50 ym-gl">
<div class="ym-fbox-text">
@Html.LabelForRequired(model => model.FirstName, null)
@Html.EditorFor(model => model.FirstName, new { required = "required", name = "firstName" })
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
but you'll have to change some things like @Html.ValidationSummary (true)
.
但你必须改变一些东西,比如@Html.ValidationSummary (true)
.
Edit
编辑
or most simple:
或者最简单的:
data: $("#registerUseraccountForm").serialize().replace("RegisterUseraccount.",""),
Edit II
编辑二
data: $("#registerUseraccountForm").serialize().replace(/RegisterUseraccount./g,""),