locked
Null Value in objLocation when Add Record RRS feed

  • Question

  • User1979860870 posted

    Hi

    $(document).ready(function () {
        //$("#delete-dialog").hide;
        $('#gvwLocation').DataTable();
        loadData();
    });
    
    function loadData() {
        $.ajax({
            url: "/Location/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.code + '</td>';
                    html += '<td>' + item.name + '</td>';
                    html += '<td>' + item.street + '</td>';
                    html += '<td>' + item.city + '</td>';
                    html += '<td>' + item.state + '</td>';
                    html += '<td>' + item.zip + '</td>';
                    html += '<td>' + item.country + '</td>';
                    html += '<td>' + item.panno + '</td>';
                    html += '<td>' + item.gstRegNo + '</td>';
                    html += '<td>' + item.mobile + '</td>';
                    html += '<td>' + item.phone_1 + '</td>';
                    html += '<td>' + item.phone_2 + '</td>';
                    html += '<td>' + item.e_Mail + '</td>';
                    html += '<td><a href="#" onclick="return getbyID(' + item.code + ')">Edit</a> | <a href="#" onclick="Delete(' + item.code + ')">Delete</a></td>';
                    html += '</tr>';
                });
                $('.tbody').html(html);
            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
    }
    
    function Add() {
        var objLocation = {
            Code: $('#Code').val(),
            Name: $('#Name').val(),
            Street: $('#Street').val(),
            City: $('#City').val(),
            State: $('#State').val(),
            Zip: $('#Zip').val(),
            Country: $('#Country').val(),
            PanNo: $('#PanNo').val(),
            GstRegNo: $('#GstRegNo').val(),
            Mobile: $('#Mobile').val(),
            Phone_1: $('#Phone_1').val(),
            Phone_2: $('#Phone_2').val(),
            E_Mail: $('#E_Mail').val(),
        };
        $.ajax({
            url: "/Location/Create",
            data: JSON.stringify(objLocation),
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (result) {
                loadData();
                $('#myModal').modal('hide');
            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
    }
    
    *************************************************
    [HttpPost]
            [ActionName("Create")]
            public ActionResult Create_Post([FromBody] Location objLocation)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        objDal.AddLocation(objLocation);
                        return new JsonResult(objLocation);
                    }
                    else
                    {
                        throw new Exception("bad data");
                        
                    }
                }
                catch (System.Data.DataException)
                {
                    ModelState.AddModelError("", "Unable to Save Changes. Try Again, and if the problem persists contact System Administrator.");
                }
                return RedirectToAction("Index");
            }

    Thanks

    Thursday, January 21, 2021 9:55 AM

All replies

  • User1686398519 posted

    Hi jagjit saini, 

    Null Value in objLocation

    1. You mean the value of objLocation is as follows:
      • City: undefined
        Code: undefined
        Country: undefined
        E_Mail: undefined
        GstRegNo: undefined
        Mobile: undefined
        Name: undefined
        PanNo: undefined
        Phone_1: undefined
        Phone_2: undefined
        State: undefined
        Street: undefined
        Zip: undefined
    2. If this is the case, I suggest that you need to check whether there is an html element whose id is Code.Only if the html element whose id is Code exists, you can get the value of the element through the jquery selector.
      • <input id="Code">
    3. If it is convenient for you, can you provide the code on your view? This can better allow us to assist you in finding the cause of the error.

    Best Regards,

    YihuiSun

    Friday, January 22, 2021 5:38 AM
  • User1979860870 posted

    Hi YihuiSun

    @model IEnumerable<Employee_Management.Models.Location>
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <div class="container">
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" onclick="clearTextBox();">Add New Location</button><br /><br />
        <table id="gvwLocation" class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>
                        Code
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        Street
                    </th>
                    <th>
                        City
                    </th>
                    <th>
                        State
                    </th>
                    <th>
                        Zip
                    </th>
                    <th>
                        Country
                    </th>
                    <th>
                        PanNo
                    </th>
                    <th>
                        GstRegNo
                    </th>
                    <th>
                        Mobile
                    </th>
                    <th>
                        Phone-1
                    </th>
                    <th>
                        Phone-2
                    </th>
                    <th>
                        EMail
                    </th>
                    <th>
                        Action
                    </th>
                </tr>
            </thead>
            <tbody class="tbody">
            </tbody>
        </table>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    @*<button type="button" class="close" data-dismiss="modal">×</button>
                    <h4 class="modal-title" id="myModalLabel">Add Employee</h4>*@
                </div>
                <div class="modal-body">
                    <form>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Code">Code</label>
                                    <input type="text" class="form-control" id="Code" placeholder="Code" />
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Name">Name</label>
                                    <input type="text" class="form-control" id="Name" placeholder="Name" />
                                </div>
                            </div>
                        </div>
    
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Code">Street</label>
                                    <input type="text" class="form-control" id="Street" placeholder="Street" />
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Name">City</label>
                                    <input type="text" class="form-control" id="City" placeholder="City" />
                                </div>
                            </div>
                        </div>
    
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Code">State</label>
                                    <input type="text" class="form-control" id="State" placeholder="State" />
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Name">Zip</label>
                                    <input type="text" class="form-control" id="Zip" placeholder="Zip" />
                                </div>
                            </div>
                        </div>
    
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Code">Country</label>
                                    <input type="text" class="form-control" id="Country" placeholder="Country" />
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Name">PanNo</label>
                                    <input type="text" class="form-control" id="PanNo" placeholder="PanNo" />
                                </div>
                            </div>
                        </div>
    
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Code">GstRegNo</label>
                                    <input type="text" class="form-control" id="GstRegNo" placeholder="GstRegNo" />
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Name">Mobile</label>
                                    <input type="text" class="form-control" id="Mobile" placeholder="Mobile" />
                                </div>
                            </div>
                        </div>
    
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Code">Phone-1</label>
                                    <input type="text" class="form-control" id="Phone_1" placeholder="Phone 1" />
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Name">Phone-2</label>
                                    <input type="text" class="form-control" id="Phone_2" placeholder="Phone 2" />
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="Code">Email</label>
                                    <input type="text" class="form-control" id="E_Mail" placeholder="Email" />
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="btnAdd" onclick="return Add();">Submit Changes</button>
                    @*<div class="form-group">
                        <div class="col-md-12 offset-md-3">
                            <input type="submit" value="Submit Changes" class="btn btn-primary btn-block" />
                        </div>
                    </div>*@
                    <button type="button" class="btn btn-primary" id="btnUpdate" style="display:none;" onclick="Update();">Update</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    
    

    Thanks

    Friday, January 22, 2021 5:40 AM
  • User1686398519 posted

    Hi jagjit saini,  

    I tested the code you provided, you can modify your code like this:

    • Delete contentType: "application/json;charset=utf-8" and dataType: "json",
     $.ajax({
          url: "@Url.Action("Create")",
          type: "POST",
          data: objLocation,
        contentType: "application/json;charset=utf-8",//delete
        dataType: "json",//delete success: function (result) { loadData(); $('#myModal').modal('hide'); }, error: function (errormessage) { alert(errormessage.responseText); } });

    Here is the result. 

    Best Regards,

    YihuiSun

    Wednesday, January 27, 2021 9:05 AM