locked
@onchange function is not working RRS feed

  • Question

  • User-1355965324 posted

    I am calling the function filldata() while doing the changes in empcode column. 

    script in cshtlm

    <div class="form-group">
    @Html.LabelFor(model => model.empCode, "Employee", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.empCode, new {htmlAttributes = new { required = "required", @class = "form-control" , @onchange = "FillData()" } })
        @Html.ValidationMessageFor(model => model.empCode, "", new { @class = "text-danger" })
    </div>
    </div>

    @section Scripts {
    <script>
    function FillData() {
    debugger
    var Id = $('#empCode').val();
    $.ajax({
    url: '/Employee/EmpolyeeEmailByCode',
    type: "GET",
    dataType: "JSON",
    data: { empcode: empCode},
    success: function (data) {
    $("#username").val(""); // clear
    $("#username").val(data);
    },
    error: function (e) {
    $("#username").val(""); // clear
    }
    });
    }


    </script>
    }

    In Controller

    public JsonResult EmpolyeeEmailByCode(string EMPREF)
    {
    if (EMPREF == null)
    {
    return Json(null);
    }
    tblEmployee tblEmployee = db.tblEmployees.Find(EMPREF);
    if (tblEmployee == null)
    {
    return Json(null);
    }
    return Json(tblEmployee.email, JsonRequestBehavior.AllowGet);
    }

    But  EmpolyeeEmailByCode  function is not calling. Any  help would be very appreciated  .  I want to get the employee email for the given empcode.

    Wednesday, August 2, 2017 3:26 PM

Answers

  • User2103319870 posted

    data: { empcode: empCode},

    You need to have the same parameter name in your controller method

    try with below code

    @section Scripts {
        <script>
            function FillData() {
                debugger
                var Id = $('#empCode').val();
                $.ajax({
                    url: '/Employee/EmpolyeeEmailByCode',
                    type: "GET",
                    dataType: "JSON",
                    data: { EMPREF: Id },
                    success: function (data) {
                        $("#username").val(""); // clear
                        $("#username").val(data);
                    },
                    error: function (e) {
                        $("#username").val(""); // clear
                    }
                });
            }
    
    
        </script>
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 2, 2017 3:43 PM