Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Js function passes null to the controller, in the old version

I need to wrote a code on an older version of the .net Framework, namely 4.5.2.
I ran into a problem, the ajax code sends an empty request to the controller.

Here is the form and I need to check user Full Name on unique:

<div class="register-card">
    <h1>Register the new user</h1>
    @using (Html.BeginForm("CreateUserAsync", "Home", FormMethod.Post))
    {
        <div>
            @Html.Label("FullName", "Enter your full name")
            <input type="text" id="FullName" name="FullName" pattern="^(\w\w+)\s(\w+)\s(\w+)$" onblur="CheckAvailability()" required />
            <span id="message" onclick="ClearMessage()"></span>
        </div>
        <p><input type="submit" value="Send" id="submit" /></p>
    }
</div>

Here is my js function to checking Full Name:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function CheckAvailability() {
    var data = $("#FullName").val();
    var param = data;
    $.ajax({
        type: "POST",
        url: "/Home/CheckFullNameAsync",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "String",
        data: JSON.stringify(param),
        success: function (response) {
            var message = $("#message");
            if (response) {
                message.css("color", "red");
                message.html("Full name is already exist");
                $('#submit').attr('disabled', 'disabled');
            }
            else {
                console.log(JSON.stringify(param));
                message.css("color", "green");
                message.html("Full name is available");
                $('#submit').removeAttr('disabled');
            }
        }
    });
};
function ClearMessage() {
    $("#message").html("");
};

Js function pass the FullName to next controller:

[HttpPost]
    public async Task<JsonResult> CheckFullNameAsync([FromBody]string fullName)
    {
        var isValid = await _service.IsUserExistAsync(fullName);
        return Json(isValid);
    }

But the controller receives null.
I think the problem is in the Js function, but I can figure out what I’m doing wrong.

>Solution :

What is dataType: "String"? That’s not a listed option. And more specifically, all you’re sending is a value but with no key. So the server isn’t able to deserialize the data and determine where the fullName value comes from.

Change the type to 'json' and send the object with the named key for the value:

dataType: "json",
data: { fullName: param },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading