【问题标题】:ASP.NET CORE 3.1 ajax post calling method but values not receivedASP.NET CORE 3.1 ajax post 调用方法但未收到值
【发布时间】:2021-08-22 03:43:28
【问题描述】:

如果我将 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");

【问题讨论】:

  • 我试过 [FromBody] 但模型参数变为空
  • 在 html 页面中已添加 AntiforgeryToken。
  • 你能显示RluModel吗?

标签: c# jquery asp.net-core asp.net-ajax razor-pages


【解决方案1】:

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

2.由于你的内容类型是 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
        });
    }

结果:

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 2021-01-18
    • 2021-09-19
    • 2021-03-11
    • 2021-05-24
    • 2016-01-14
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多