ActionLink jQuery 参数 [英] ActionLink jQuery parameter

查看:21
本文介绍了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".

添加新报告"链接非常简单……它在我的控制器中调用一个 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.

我找到了一篇文章,向我展示了如何对我的 ActionLink 进行 AJAX 化,但我做错了……

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点击事件

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

在 Controller 的方法中放置断点时,它被命中并且参数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".

在点击事件中移除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点击事件:

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

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

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天全站免登陆