javascript 使用 js 从视图调用 ActionResult 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22635287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:29:36  来源:igfitidea点击:

Calling ActionResult method from the View using js

javascriptjqueryasp.net-mvcmodel-view-controller

提问by Greeed

So I am calling my action result method using this:

所以我使用这个调用我的操作结果方法:

var url= "/Example/Controler/1/Action";

$("<form action='"+url+"'></form>").submit();

But the action Method is not called... I have also tried this

但是没有调用动作方法...我也试过这个

$.post(url, function (data) {}); 

And this works, we call the controller, but then the page doesn't refresh...

这有效,我们调用控制器,但页面不刷新......

My action method looks like this:

我的操作方法如下所示:

public ActionResult DoStuff(int Id)
    {   
     .....
     return   RedirectToAction("index", new { Id });
    }

回答by Hamid Narikkoden

You can use Ajax function as follows :

您可以按如下方式使用 Ajax 函数:

$.ajax({
    url: "/Controler/Action",
    data: { 'Id': groupId },
    type: 'GET',
    success: function (result) {
       //do the necessary updations
    },
    error: function (result) {
    }
});

You can also try form submission as follows :

您还可以尝试如下提交表单:

@using (Html.BeginForm("Action", "Controller", FormMethod.GET))
{
       // do your html
       <input type="submit" value="save"/>
}