简体   繁体   English

使用 jquery 调用 controller 方法,但方法未重定向到其相应视图

[英]calling controller method using jquery but method not redirecting to its corresponding view

I have a page " GetData.cshtml " where I need to fill up some textbox data and send to the controller method.我有一个页面“ GetData.cshtml ”,我需要在其中填写一些文本框数据并发送到 controller 方法。 I am using jquery to send data to the controller method.我正在使用 jquery 将数据发送到 controller 方法。

//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.在调试应用程序时,成功调用了controller方法。

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.根据我的要求,我想在Registration_card.cshtml页面中显示数据,但它没有重定向到页面。 The console message console.log("job done....");控制台消息console.log("job done...."); in the jquery block is showing.在 jquery 块中显示。 It means the page is again back to GetData.cshtml .这意味着页面又回到了GetData.cshtml Is it due to using jquery to call controller method?是因为使用 jquery 调用 controller 方法吗? How can I go to page Registration_card.cshtml after calling the controller method Registration_card via jquery.如何通过 jquery 调用 controller 方法Registration_card后 go 到页面Registration_card.cshtml

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; }
        }

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 .根据您的问题,您似乎想从GetData.cshtml重定向/转到Registration_card.cshtm ,方法是从GetData.cshtml获取一些数据到Registration_card ActionMethod ,这实际上会显示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 .如果上述情况属实,那么您不需要 Ajax Post 请求,因为您的ActionMethodHttpGet ,因为您没有指定它是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....");
    });
})

If want to redirect page you should use javascript or jquery because when you use jquery then controller not working for redirect.如果要重定向页面,您应该使用 javascript 或 jquery,因为当您使用 jquery 时,Z594C103F2C6E04EC 重定向无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM