locked
How can prevent the calling of [HttpPost] method while button click RRS feed

  • Question

  • User-1355965324 posted

     When clicking the button of the partial view , how can I  prevent the calling of  [HttpPost] method in the controller, Please help me with suggested code and would be very appreciated

    I have a saleshtml file,   from there I am calling the partial view popup for searching and selecting the customer  code to the sales.html file. It is working but  after the selection and placing  the customer code from the partial view to the sales.html  file, It is automatically calling HttpPost method of salescontroller and then all the  given value in other cntrol of the sales.html  will be disappeared.  How can I  resolve the removing of previously given data,   please any help would be very appreciated.  After  the selection of customer code , I am closing  the partial view but immediatly it will go  to httppost and the view would be recalled again . So All the values given in the  Sales  view file  would be gone except customer code. Please help 

    SALES CONTROLLER

    public IActionResult sales(int DN = 0, int DPT = 0, int EID = 0, int MNO=0)
            {
                 
                    Salesmodel salesmodel = new Salesmodel();
                    ViewBag.UserDepots = EmployeeService.GetUserDepots(HttpContext.Session.GetInt32("UserID") ?? 0, connectionSettings);
                    ViewBag.UserDepartments = EmployeeService.GetUserDepartments(HttpContext.Session.GetInt32("UserID") ?? 0, connectionSettings);  
     
                        return View(salesmodel);
     
            }
    
     [HttpPost]
            public IActionResult sales(Salesmodel  salesmodel)
            {
                return View(salesmodel);
            }
    
    public IActionResult SearchCustomer()
            {
                CustomerModel Customer = new CustomerModel();
                ViewBag.Customers  = EmployeeService.GetCameoCustomer(connectionSettings);
                return PartialView("CustomerSearch");
                
            }
    

    Sales.cshtml

    <div class="col-sm-3">
                            <select id="dropdownDepot" class="form-control" asp-for="DepotNo" asp-items="@ViewBag.UserDepots"
                                    data-role="select" onchange="FillEmployeeDropdown()"></select>
                            <span asp-validation-for="DepotNo" class="text-danger"></span>
                        </div>
                        <label for="lblDepartment" class="control-label col-sm-1 ">Department</label>
                        <div class="col-sm-3">
                            <select id="dropdownDepartment" class="form-control" asp-for="DepartmentID" asp-items="@ViewBag.UserDepartments"
                                    data-role="select" onchange="FillEmployeeDropdown()"></select>
                            <span asp-validation-for="DepartmentID"></span>
                        </div>
    <div class="form-group">
                        <label for="lblCustomer" class="control-label col-sm-1">Customer</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" asp-for="CustomerCode" id="lblCustomer">
                                <button type="button" class="btn btn-primary form-control" onclick="ShowCustomerSearch()">Search</button>
                            </div>
                        <div id="CustomerSearchModal" class="modal fade" role="dialog">
                                <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <a href="#" class="close" data-dismiss="modal">&times;</a>
                                                <h3 class="modal-title">Customer Search</h3>
                                            </div>
                                            <div class="modal-body" id="CustomerSearchDiv">
    
                                            </div>
                                        </div>
    
                                </div>
                        </div>
    </div>
    
    
    <script type="text/javascript">
    
        function ShowCustomerSearch() {
            var url = "/Employee/SearchCustomer";
    
            $("#CustomerSearchDiv").load(url, function () {
                $("#CustomerSearchModal").modal("show");
            })
        }
    </script>
    
    
    

    CustomerSearch.cshtml

    <table id="CustomerSearch" class="display" style="width:100%">
                        <thead>
                            <tr>
                                <th>Customer Code</th>
                                <th>Customer Name</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (CustomerModel customer in ViewBag.Customers as List<CustomerModel>)
                            {
                            <tr>
                                <td> @customer.CustomerCode</td>
                                <td> @customer.CustomerName</td>
    
                            </tr>
    
                            }
                        </tbody>
    
    
                    </table>
    
    <script type="text/javascript">
    
        $(document).ready(function () {
            var table = $('#CustomerSearch').DataTable({
                scrollY: "500px",
                scrollX: true,
                paging: true,
                "lengthMenu": [[100, -1], [100, "All"]],
                dom: 'Bfrtip',
                select: true,
                
    
            }
    
            );
            var table = $('#CustomerSearch').DataTable();
            $('#CustomerSearch tbody').on('click', 'tr', function () {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                }
                else {
                    table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
    
    
    
            });
    
    
    $(".btn").on('click', function () {
                var idx = table.cell('.selected', 0).index();
                //var data = table.rows(idx.row).data();
                var data = table.cell(idx, 0).data();
                $('#txtCustomer').val(data);  // Here placing the value from customersearch
                $("#CustomerSearchModal").modal("hide");
                $(".modal-backdrop").removeClass("in").addClass("out");
                $(".modal-backdrop").fadeOut();           
                
            });
            
    
    
        });
    

    Wednesday, July 31, 2019 1:00 PM

Answers

  • User-1355965324 posted

    I sorted  the problem by passing the value to the model

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 31, 2019 2:20 PM

All replies

  • User475983607 posted

    It is automatically calling HttpPost method of salescontroller and then all the  given value in other cntrol of the sales.html  will be disappeared. 

    HTTP request do not automatically happen.   HTTP posts happen due to code that you write. 

    The symptom points to submitting a form post which causes the bower to refresh the page.  You did not post the form element so this is just a guess.

    You have been on these forums for a long time and, IMHO, it is about time you learn how to debug your code using the Visual Studio debugger and the browser's Dev Tools.  It is up to you to design code that functions properly according to HTML Web Forms and MVC fundamentals.  Using the debuggers will help you to compare how your code actually functions to how you expect the code to function.

    Wednesday, July 31, 2019 1:34 PM
  • User-1355965324 posted

    I sorted  the problem by passing the value to the model

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 31, 2019 2:20 PM