简体   繁体   English

Asp.Net Core Razor Page PUT处理程序模型绑定程序未绑定Ajax上Java脚本的序列化表格

[英]Asp.Net Core Razor Page PUT Handler model binder not binding the serialized form from java script on ajax put

I'm trying to serialize the form and put it to server and for some reason the model binding fails to bind the model. 我正在尝试序列化表单并将其放入服务器,由于某种原因,模型绑定无法绑定模型。

Here is my javascript button click, 这是我的javascript按钮单击,

$.when(ajaxFormPutAsync(window.userUrls.update, $('#formUserEdit').serialize(), true)).done((response) => {

       });

ajaxFormPutAsync method: ajaxFormPutAsync方法:

function ajaxFormPutAsync(requestUrl, putData, global) {
    try {
        return $.ajax({
            type: "PUT",
            url: requestUrl,
            async: true,
            global: global,
            headers: {
                "XSRF-TOKEN": $('input[name="__RequestVerificationToken"]').val()
            },
            data: putData,
            error: function (jqXhr, textStatus, errorThrown) {
                console.log(jqXhr);
            }
        });
    } catch (q) {
        ajaxIndicatorStop();
    }

    return false;
};

Here is my form, 这是我的表格,

<form id="formUserEdit" method="put">
    <div class="form-row">
        <div class="form-group col-md-6">
            <label asp-for="FirstName"></label>
            <input asp-for="FirstName" class="form-control form-control-sm">
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
        <div class="form-group col-md-6">
            <label asp-for="LastName"></label>
            <input asp-for="LastName" class="form-control form-control-sm">
            <span asp-validation-for="LastName" class="text-danger"></span>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-6">
            <label asp-for="Designation"></label>
            <input asp-for="Designation" class="form-control form-control-sm">
            <span asp-validation-for="Designation" class="text-danger"></span>
        </div>
        <div class="form-group col-md-6">
            <label asp-for="IdentityNumber"></label>
            <input asp-for="IdentityNumber" class="form-control form-control-sm">
            <span asp-validation-for="IdentityNumber" class="text-danger"></span>
        </div>
    </div>
    <input type="hidden" asp-for="Id" />
</form>

Here is my RazorPageHandler , 这是我的RazorPageHandler

public async Task<IActionResult> OnPutAsync(
    [FromBody] UserViewModel userViewModel,
    CancellationToken ct)
{
...

Here is my UserViewModel , 这是我的UserViewModel

    public class UserViewModel
    {
        public Guid Id { get; set; }

        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required]
        public string Designation { get; set; }

        [Required]
        [Display(Name = "Identity Number")]
        public string IdentityNumber { get; set; }
    }

the request is hitting my page handler but userViewModel is null always. 该请求正在击中我的页面处理程序,但userViewModel始终为null

Any suggestion on where I'm wrong? 有什么建议我错了吗?

Please assist. 请协助。

Parameter Binding is very specific in Asp.Net meaning that if you intend to POST data from your front-end, with either ajax or the form itself, you have to make sure that it is in the correct format that your controller action anticipates receiving the data. 参数绑定是非常具体的Asp.Net这意味着,如果你打算POST从前端数据,无论是用AJAX或窗体本身,你必须确保它是在你的控制器动作预计接收正确的格式数据。

Therefore, since you are submitting your data like this $('#formUserEdit').serialize() , ajax will serialize it as FormData and your controller action should receive it in the same format. 因此,由于您要像$('#formUserEdit').serialize()这样提交数据,因此ajax会将其序列化为FormData并且您的控制器操作应以相同的格式接收它。

Solution

Change controller action from [FromBody] to [FromForm] and that should work. 将控制器操作从[FromBody]更改为[FromForm] ,这应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM