none
ASP.NET Core - Passing parameter null

    Question

  • I have problem with my code right now.  It seems it doesn't pass a value to a controller.  See my code below:

    Controller:

    [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Delete(Employee emp)
            {
                try
                {
                    ModelEmployee empDelete = new ModelEmployee();
    
                    empDelete.employees = await _Context.Employee.FirstOrDefaultAsync(e => e.EmployeeId == emp.EmployeeId);
                    _Context.Employee.Remove(empDelete.employees);
    
                    await _Context.SaveChangesAsync();
    
                    return RedirectToAction("Employee/Index");
                }
                catch (Exception)
                {
    
                    throw;
                }
            }




    View:

    This is the button where a dialog open using bootstrap and pass the parameter to the modal.

    <button type="button" class="btn btn-danger btn-sm openDiag" data-toggle="modal" data-target="#myModal" data-emp-id="@item.EmployeeId"><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Confirm Delete</h4>
                </div>
                <div class="modal-body">
                    <!-- The problem seems to be here:-->
                    <p>Are you sure you want to delete invoice number:</p>
                </div>
                <div class="modal-footer">
                    <form asp-controller="Employee" asp-action="Delete" method="post" class="form-inline" role="form">
                        <input type="hidden" id="empID">
                        <button type="submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </form>
                </div>
            </div>
        </div>
    </div> 

    This script will pass the parameter directly to the modal:

    <script type="text/javascript">
        $(document).on("click", ".openDiag", function () {
            var empid = $(this).data("emp-id");
            $(".modal-footer #empID").val(empid);
        });
    </script>

    The error shows a null value on it even though in the i set the form value on input box which handles the value.

    Can someone help me with it?


    Sunday, April 2, 2017 9:41 AM

Answers

  • Hi,

    Two things:

    First ASP.NET (Razor) wants you to use the 'name' tag rather than the id tag to identify fields for mapping.

    Second, even if it did take the id tag, you are naming your hidden field 'empId' and looking for 'emp.EmployeeId' for comparison in your controller code.

    Fix both of those and you should be good to go.

    Regards,

    D

    ___

    Please mark as answer if this helped you

    • Proposed as answer by bowman_d Sunday, April 2, 2017 9:57 AM
    • Marked as answer by ajqportal Sunday, April 2, 2017 10:19 AM
    Sunday, April 2, 2017 9:54 AM

All replies

  • Hi,

    Two things:

    First ASP.NET (Razor) wants you to use the 'name' tag rather than the id tag to identify fields for mapping.

    Second, even if it did take the id tag, you are naming your hidden field 'empId' and looking for 'emp.EmployeeId' for comparison in your controller code.

    Fix both of those and you should be good to go.

    Regards,

    D

    ___

    Please mark as answer if this helped you

    • Proposed as answer by bowman_d Sunday, April 2, 2017 9:57 AM
    • Marked as answer by ajqportal Sunday, April 2, 2017 10:19 AM
    Sunday, April 2, 2017 9:54 AM
  • Hi @bowman_d, what do you mean by 2nd one? do you mean that I need to name it same as the emp.EmployeeId on the naming tag? Like this instead using empId I will use emp.EmployeeId on the name tag? Sorry I don't really understand English that much.  
    • Edited by ajqportal Sunday, April 2, 2017 10:07 AM correction
    Sunday, April 2, 2017 10:05 AM
  • Hi

    In your controller code, you are accepting a class of type Employee - this is the model that will be passed from your view to your controller. Each property in the Model must have the same name as the input elements in the view.

    So if you have an <input type="hidden" name="propertyName"/> the propertyName must match the name of the property in the Model class. If it doesn't, the MVC engine wont be able to decide what fields to match to what property. In the end you'll end up with a bunch of fields with null or default values.

    Regards,

    D

    ___

    Please mark as answer if this helped you

    Sunday, April 2, 2017 10:10 AM