ASP.NET CORE 3.1 ajax post 调用方法但未收到值

ASP.NET CORE 3.1 ajax post calling method but values not received

如果我将 Ajax 类型更改为 GET,它可以工作,但要求是使用 POST 类型。

这是在 razor 页面中创建的方法。

public JsonResult OnPostRLUAddUpdate(RluModel model)
{
    model.LastModifiedBy = User.Identity.Name;
    var obj = _iRLURepo.RLUAddUpdate(model);
    return new JsonResult(obj.Result);
}

Ajax代码:

  var model = {
                "RLUID": $("#RLUID").val(),
                "RLUNo": $("#RLUNo").val(),
                "RLUAcres": $("#RLUAcres").val(),
                "TractName": $("#TractName").val(),
                "CountyID": $("#CountyID").val(),
                "ClientPropertyID": $("#ClientPropertyID").val(),
                "DisplayDescription": $("#DisplayDescription").val(),
                "InternalNotes": $("#InternalNotes").val()
            }
            $.ajax({
                type: 'Post',
                url: 'RLU?handler=RLUAddUpdate',
               // async: true,
                data: JSON.stringify({ model }),
                headers: { "XSRF-TOKEN": $('input:hidden[name="__RequestVerificationToken"]').val() },
                //contentType: "application/json; charset=utf-8",
                contentType: 'application/x-www-form-urlencoded',
                dataType: "json",
                success: OnRLUAddUpdateSuccess,
                complete: OnCompleteRLU,
                error: OnErrorRLU
            });

Startup.cs

services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");

1.If你在RluModel中有int类型,你需要在js中使用parseInt$("#xxx").val()转换为int类型。

2.And 因为你的内容类型是 contentType: 'application/x-www-form-urlencoded',,你只需要使用data: model,。 这是一个演示:

RluModel(由于不知道你的RluModel的结构,所以用下面的代码来测试):

public class RluModel
    {
        public int RLUID { get; set; }
        public int RLUNo { get; set; }
        public string TractName { get; set; }
        public int CountyID { get; set; }
        public int ClientPropertyID { get; set; }
        public string DisplayDescription { get; set; }
        public string InternalNotes { get; set; }

    }

查看:

@Html.AntiForgeryToken()
<form>
    <div class="form-group">
        <label class="control-label">RLUID</label>
        <input id="RLUID" class="form-control" />
    </div>
    <div class="form-group">
        <label class="control-label">RLUNo</label>
        <input id="RLUNo" class="form-control" />
    </div>
    <div class="form-group">
        <label class="control-label">RLUAcres</label>
        <input id="RLUAcres" class="form-control" />
    </div>
    <div class="form-group">
        <label class="control-label">TractName</label>
        <input id="TractName" class="form-control" />
    </div>
    <div class="form-group">
        <label class="control-label">CountyID</label>
        <input id="CountyID" class="form-control" />
    </div>
    <div class="form-group">
        <label class="control-label">ClientPropertyID</label>
        <input id="ClientPropertyID" class="form-control" />
    </div>
    <div class="form-group">
        <label class="control-label">DisplayDescription</label>
        <input id="DisplayDescription" class="form-control" />
    </div>
    <div class="form-group">
        <label class="control-label">InternalNotes</label>
        <input id="InternalNotes" class="form-control" />
    </div>
    <button onclick="postdata()">submit</button>
</form>

js:

function postdata() {
        var model = {
            "RLUID": parseInt($("#RLUID").val()),
            "RLUNo": parseInt($("#RLUNo").val()),
            "RLUAcres": $("#RLUAcres").val(),
            "TractName": $("#TractName").val(),
            "CountyID": parseInt($("#CountyID").val()),
            "ClientPropertyID": parseInt($("#ClientPropertyID").val()),
            "DisplayDescription": $("#DisplayDescription").val(),
            "InternalNotes": $("#InternalNotes").val()
        }
        $.ajax({
            type: 'Post',
            url: 'RLU?handler=RLUAddUpdate',
            // async: true,
            data: model,
            headers: { "XSRF-TOKEN": $('input:hidden[name="__RequestVerificationToken"]').val() },
            //contentType: "application/json; charset=utf-8",
            contentType: 'application/x-www-form-urlencoded',
            dataType: "json",
            success: OnRLUAddUpdateSuccess,
            complete: OnCompleteRLU,
            error: OnErrorRLU
        });
    }

结果: