gpt4 book ai didi

asp.net-mvc - 与 jquery ajax 序列化的模型绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-12-03 22:24:08 24 4
gpt4 key购买 nike

我有以下模型:

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; }

...
}

以及以下 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>
...

和我的 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.');
}
});

这是序列化的结果:

RegisterUseraccount.FirstName=Peter&RegisterUseraccount.LastName=Miller&RegisterUseraccount.Email=miller%40gmail.com&RegisterUseraccount.Password=admin

这是我尝试发布到的 Controller 方法:

[HttpPost]
public ActionResult Register(RegisterUseraccount registerUseraccount)
{
...
}

...但是数据没有到达该方法,我收到错误 404。我认为 modelbinder 无法工作。

有效的是使用名称firstName=Peter发送的数据,但实际发送的是RegisterUseraccount.FirstName=Peter。

我该如何解决这个问题?

最佳答案

删除 contentType: 'application/json', 并将其修改为更好(从我的角度来看)

$('#registerUseraccountForm').submit(function () {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
beforeSend: function () {

},
complete: function () {

},
...

关于asp.net-mvc - 与 jquery ajax 序列化的模型绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16717715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com