【问题标题】:Calling ActionResult method from the View using js使用js从视图调用ActionResult方法
【发布时间】:2014-03-25 12:55:03
【问题描述】:

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

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

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

但是action Method没有被调用... 这个我也试过了

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

这行得通,我们调用了控制器,但是页面没有刷新...

我的操作方法如下所示:

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

【问题讨论】:

  • 您真的在提交数据吗? (&lt;form&gt; 是必需的吗?)另外,你的行为是否偶然被HttpPost 修饰了?
  • 在$.post()方法成功函数中,需要更新页面内容
  • @Brad Christie nope no httppost

标签: javascript jquery asp.net-mvc model-view-controller


【解决方案1】:

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

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

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

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

【讨论】:

  • //做必要的更新 = document.location.reload(true);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多