加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ActionLink jQuery参数

发布时间:2020-12-16 07:35:45 所属栏目:asp.Net 来源:网络整理
导读:我已经创建了一个ASP.NET MVC 2.0应用程序. 我有一个带有“报告”列表的下拉框.在下拉列表旁边,我有两个ActionLink.一个说“添加新报告”,另一个说“编辑报告”. “添加新报告”链接非常简单……它在我的Controller中调用ViewResult并返回一个新的View().大
我已经创建了一个ASP.NET MVC 2.0应用程序.

我有一个带有“报告”列表的下拉框.在下拉列表旁边,我有两个ActionLink.一个说“添加新报告”,另一个说“编辑报告”.

“添加新报告”链接非常简单……它在我的Controller中调用ViewResult并返回一个新的View().大!这没问题!

“编辑报告”链接有点棘手,因为我希望将下拉列表中当前所选项目的选定ID传递给ActionLink.

我发现了一篇文章,告诉我如何AJAX化我的ActionLink,但我做错了…

以下是“编辑”链接视图中的ActionLink:

<%=Html.ActionLink("Edit report","EditReport","Report",null,new { id = "edit" })%>

这是用于处理“点击”的jQuery点击事件

$("#edit").click(function() {
   $.ajax({
     url: '<%=Url.Action("EditReport")%>',type: 'POST',data: { reportId: $('select[name="ReportId"] option:selected').val() },success: function(result) {
          //alert("succes");
     },error: function() {
          alert("error");
     }
     });
   return false;
});

这是Controller中的方法:

public ViewResult EditReport(int reportId)
{
      return View("EditReport");
}

在Controller的方法中放置一个断点时,它会被命中并且参数“reportId”被正确传递…但其余代码(返回View()部分)似乎不起作用,因为在jQuery click事件中,我有一个“回归虚假”.

在click事件中删除“return false”时,断点不再受到攻击.因此,我不能去我的“EditReport”视图……

我在这里缺少什么/不理解?

另外……有没有更好/更好/更清洁的方法来实现我的任务而无需使用AJAX调用?

解决方法

好的,因为我现在可以在这里发布答案(如果有人有兴趣):

首先,我需要修改ActionLink并为参数添加预定义值,一旦单击链接,该参数将被替换.

<%=Html.ActionLink("Edit report",new { reportId="xxx"},new { id = "edit" })%>

其次,修改jQuery click事件:

$("#edit").click(function() {
    //Get the id of the selected item in dropdown
    var id = $('select[name="ReportId"] option:selected').val();

    //Replace the predifined QueryString param "xxx" with the "id"
    this.href = this.href.replace("xxx",id);
});

Controller的方法没有任何变化……它保持不变:

public ViewResult EditReport(int reportId)
{
  //Fix me...
   return View("EditReport");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读