ActionLink jQuery 参数

发布于 2024-12-11 05:23:27 字数 1286 浏览 0 评论 0原文

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

我有一个带有“报告”列表的下拉框。在下拉菜单旁边,我有两个 ActionLink。一个写着“添加新报告”,另一个写着“编辑报告”。

添加新报告”链接非常简单……它在我的控制器中调用 ViewResult 并返回一个 new View()。伟大的!这没问题!

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

我找到了一篇文章,向我展示了如何 AJAXify 我的 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;
});

这是控制器中的方法:

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

当在控制器的方法中放置断点时,它会被击中并且参数“reportId”被正确传递......但其余代码(返回 View() 部分)没有似乎有效,因为里面jQuery 点击事件,我有一个“返回 false”。

当删除点击事件中的“return false”时,断点不再被命中。因此,我无法进入我的“EditReport”视图……

我在这里错过/不理解什么?

另外……有没有更好/更好/更干净的方法来完成我的任务而无需使用 AJAX 调用?

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

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”.

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!

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.

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

Here is the ActionLink in the View for the “Edit” link:

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

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

Here is the method in the Controller:

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

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”.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她如夕阳 2024-12-18 05:23:27

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

首先,我需要修改 ActionLink 并放入预定义< /strong> 单击链接后将被替换的参数值。

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

其次,修改 jQuery 单击事件:

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

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

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

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

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

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");
}
奶茶白久 2024-12-18 05:23:27

您可以执行类似的操作,前提是您已为 ControllerName/EditReports/{ReportId} 配置了路由路径

$("#edit").click(function() {
      window.location.href = '<%=Url.Action("EditReport")%>' +'/'+ $('select[name="ReportId"] option:selected').val();
      return false;
 });

You can do something like this, provided you have configured a route path for ControllerName/EditReports/{ReportId}

$("#edit").click(function() {
      window.location.href = '<%=Url.Action("EditReport")%>' +'/'+ $('select[name="ReportId"] option:selected').val();
      return false;
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文