ActionLink jQuery参数 [英] ActionLink jQuery parameter

查看:75
本文介绍了ActionLink jQuery参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个ASP.NET MVC 2.0应用程序.

I've created an ASP.NET MVC 2.0 application.

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

I have a dropdownbox with a list of "reports". Next to the dropdown, I have two ActionLink. One that says "Add new report" and the other one say "Edit report".

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

The "Add new report" link, is pretty straightforward … it calls a ViewResult in my Controller and returns a new View(). Great! this works no problem!

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

The "Edit report" link is a bit trickier since I wish to pass the selected ID of the item currently selected inside the dropdown to the ActionLink.

我找到了一篇文章,向我展示了如何AJAXify我的ActionLink,但我做错了事...

I've found an article that shows me how to AJAXify my ActionLink but I’m doing something wrong…

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

Here is the ActionLink in the View for the "Edit" link:

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

这是jQuery click事件,用于处理点击"

Here is the jQuery click event to handle the "click"

$("#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;
});

这是控制器中的方法:

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

在控制器的方法中放置断点时,它会被命中,并且参数" reportId "已正确传递……但是其余代码(返回View()部分)似乎不起作用因为在jQuery click事件中,我有一个"return false".

When placing a breakpoint in the Controller’s method, it gets hit and the parameter "reportId" is correctly passed…but the rest of the code (return View() portion) does not seem to work because inside the jQuery click event, I have a "return false".

当删除click事件中的"return false"时,将不再命中断点.因此,我无法进入"EditReport"视图…

When removing the "return false" inside the click event, the breakpoint no longer gets hit. Thus, I can't go to my "EditReport" view…

我在这里想念/不了解什么?

What am I missing/not understanding here?

还…在没有使用AJAX调用的情况下,是否有更好/更精细/更干净的方法来完成我的任务?

Also … is there a better/nicer/cleaner approach to achieve my task without having to use an AJAX call?

推荐答案

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

Ok, since I now can post the answer here it is (if anyone is interested):

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

First, I needed to modify the ActionLink and put in a predefined value for the parameter which will be replaced once the link is clicked.

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

第二,修改jQuery click事件:

Second, modify the jQuery click event:

$("#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);
});

控制器的方法没有任何变化……保持不变:

Nothing changes in the Controller’s method…it stays the same:

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

这篇关于ActionLink jQuery参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆