Search code examples
c#jqueryasp.net-coreasp.net-ajaxrazor-pages

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


If I will change Ajax type to GET, it works, but requirement is to use POST type.

Here is method created in razor page.

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

Ajax code:

  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");

Solution

  • 1.If you have int type in RluModel,you need to use parseInt to convert $("#xxx").val() to int type in js.

    2.And since your content type is contentType: 'application/x-www-form-urlencoded',,you only need to use data: model,. Here is a demo:

    RluModel(Since I don't know the structure of your RluModel,so I use the following code to test):

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

    View:

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

    result: enter image description here