简体   繁体   中英

Null Value while passing json data to mvc 6 web application using knockout.js

Using the knockout js I tried to pass the json data to the mvc 6 controller action. I can see only null value to the dto parameter. If I use the same method in mvc 5 the value is assign to the dtos. What I am missing in the code.

var LoginAuthentication = {

    username: ko.observable(),
    password: ko.observable(),
    GetLoginAuthentication: function () {

        if ($("#loginAuthentication").valid()) {
            var self = this;
            var ajaxUrl = ApplicationRootUrl("LoginAuthentication", "Home");
            var UserCrendential = {
                UserName: self.username(),
                Password: self.password()
            };
            console.log(ko.toJSON(UserCrendential));
            $.ajax({
                type: "POST",
                url: ajaxUrl,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: ko.toJSON(UserCrendential),
                success: function (data) {
                },
                error: function (err) {

                }

            });
        }
    }
};

Here is the dto class

public class LoginAuthenticationModel
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }

Below is the controller method.

 public void LoginAuthentication(LoginAuthenticationModel loginAuthenticationModel)
        {
            if (ModelState.IsValid)
            {
                try
                {

                }
                catch (Exception exception)
                {


                }
            }
        }

The output result of the code 在此输入图像描述

The result of browser inspect 在此输入图像描述

Include [FromBody] in your controller to read json data from request body as

public void LoginAuthentication([FromBody]LoginAuthenticationModel loginAuthenticationModel)
{
    if (ModelState.IsValid)
    {
    }
}

The below line solve the problem however I dont known is it a right solution. I remove the below code from ajax call dataType: "json"

and changes the data as loginAuthenticationModel without converting to anything

var LoginAuthentication = {

    username: ko.observable(),
    password: ko.observable(),
    GetLoginAuthentication: function () {

        if ($("#loginAuthentication").valid()) {
            var self = this;
            var ajaxUrl = ApplicationRootUrl("LoginAuthentication", "Home");
            var loginAuthenticationModel = {
                UserName: self.username(),
                Password: self.password()
            };
            $.ajax({
                type: "POST",
                url: ajaxUrl,
                dataType: "json",
                data: loginAuthenticationModel,
                success: function (data) {
                },
                error: function (err) {

                }

            });
        }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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