asp.net-mvc – 使用jquery进行模型绑定ajax序列化不起作用

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用jquery进行模型绑定ajax序列化不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下型号:
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; }

    ...
}

以下观点:

@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

这是我试图POST到的控制器方法

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

…但是数据没有到达方法,我收到错误404.我认为模型绑定器无法工作.

什么有效是以名称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 () {

            },...
原文链接:https://www.f2er.com/aspnet/249259.html

猜你在找的asp.Net相关文章