locked
Null Value in Controller RRS feed

  • Question

  • User-797751191 posted

    Hi

      In below code alert displays correct value but in Controller it shows null

    function getbyName(UsrName) {
        $('#UserName').css('border-color', 'lightgrey');
        $('#Password').css('border-color', 'lightgrey');
        $('#FirstName').css('border-color', 'lightgrey');
        $('#LastName').css('border-color', 'lightgrey');
        alert(UsrName);
        $.ajax({
            url: '/Home/GetbyName',
            data: JSON.stringify({ UsrName: UsrName }),
            type: "GET",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: function (result) {
                $('#UserName').val(result.UserName);
                $('#Password').val(result.Password);
                $('#FirstName').val(result.FirstName);
                $('#LastName').val(result.LastName);
    
                $('#myModal').modal('show');
                $('#btnUpdate').show();
                $('#btnAdd').hide();
            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
        return false;
    }
    
    function loadData() {
        $.ajax({
            url: "/Home/List",
            type: "GET",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (result) {
                var html = '';
                $.each(result, function (key, item) {
                    html += '<tr>';
                    html += '<td>' + item.UserName + '</td>';
                    html += '<td>' + item.Password + '</td>';
                    html += '<td>' + item.FirstName + '</td>';
                    html += '<td>' + item.LastName + '</td>';
                    html += '<td><a href="#" onclick="return getbyName(\'' + item.UserName + '\')">Edit</a> | <a href="#" onclick="Delete(\'' + item.UserName + '\')">Delete</a></td>';
                    html += '</tr>';
                });
                $('.tbody').html(html);
            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
    }
    
    public JsonResult GetbyName(string UsrName)
            {   
                var UserName = usrDB.GetAllUser().Find(x => x.UserName.Equals(UsrName));
                return Json(UserName, JsonRequestBehavior.AllowGet);
            }

    Thnaks

    Wednesday, October 23, 2019 5:10 PM

Answers

  • User-17257777 posted

    Hi jsshivalik,

    data: JSON.stringify({ UsrName: UsrName }),

    I am a little confused why you should use JSON.stringify here. It is used to convert a JavaScript object into a string. Just put a { key : value } format data here:

    data: { UsrName: UsrName },

    The first UsrName here is the key, which is corresponding to the receiving parameter in the controller action, and the second one is the specific value.

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 24, 2019 2:42 AM

All replies

  • User1120430333 posted

     /Home/GetbyName?UsrName=" + '$('#UserName').val(result.UserName).ToString();

    You can't something like above? It's been sometime since I have used Jquery

    And Username is spelled wrong in the action method too.

    Wednesday, October 23, 2019 5:58 PM
  • User-17257777 posted

    Hi jsshivalik,

    data: JSON.stringify({ UsrName: UsrName }),

    I am a little confused why you should use JSON.stringify here. It is used to convert a JavaScript object into a string. Just put a { key : value } format data here:

    data: { UsrName: UsrName },

    The first UsrName here is the key, which is corresponding to the receiving parameter in the controller action, and the second one is the specific value.

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 24, 2019 2:42 AM