Home > Net >  calling controller method using jquery but method not redirecting to its corresponding view
calling controller method using jquery but method not redirecting to its corresponding view

Time:12-22

I have a page "GetData.cshtml" where I need to fill up some textbox data and send to the controller method. I am using jquery to send data to the controller method.

//GetData.cshtml
$(document).ready(function () {
    $('#btn_sbmt_recommendation').click(function () {            
        $.post("/RegistrationModels/Registration_card",
        {
            firm_id: $('#id_reg_id').val(), 
            amount: $('#id_amount').val()
            });
        console.log("job done....");
    });
})

While debugging the app, the controller method is called successfully.

public ActionResult Registration_card(string reg_id, string amount)
        {
            try
            {
                // code block
                return View(updated_firm);
            }
            catch (Exception ex) { throw; }
        }
    

As my requirement, I want to display the data in Registration_card.cshtml page, but it is not redirecting to the page. The console message console.log("job done...."); in the jquery block is showing. It means the page is again back to GetData.cshtml. Is it due to using jquery to call controller method? How can I go to page Registration_card.cshtml after calling the controller method Registration_card via jquery.

I also tried with the below code, but it is also not working.

public ActionResult Registration_card(string firm_id, string ward_calani_num, string amount)
        {
            try
            {
                // code block
                return RedirectToAction("Registration_card", updated_firm);
            }
            catch (Exception ex) { throw; }
        }

CodePudding user response:

According to your question, it seems you want to redirect/go to the Registration_card.cshtm from GetData.cshtml by taking some data from the GetData.cshtml to the Registration_card ActionMethod which will actually show the Registration_card.cshtm.

If the above scenario is true, then you do not need to Ajax Post request as your ActionMethod is HttpGet as you do not specify it is as HttpPost. So the workaround can be something like the below code.

$(document).ready(function () {
    $('#btn_sbmt_recommendation').click(function () {
        var id_reg_id=$('#id_reg_id').val();   
        var id_amount=$('#id_amount').val();         
        location.href=`/RegistrationModels/Registration_card?reg_id=${id_reg_id}&amount=${id_amount}`;
      
        //$.post("/RegistrationModels/Registration_card",
           //    {
           //        firm_id: $('#id_reg_id').val(),
           //        amount: $('#id_amount').val()
           //    });
          //console.log("job done....");
    });
})

CodePudding user response:

If want to redirect page you should use javascript or jquery because when you use jquery then controller not working for redirect.

  • Related