ActionLink jQuery 参数
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,既然我现在可以在这里发布答案(如果有人感兴趣):
首先,我需要修改 ActionLink 并放入预定义< /strong> 单击链接后将被替换的参数值。
其次,修改 jQuery 单击事件:
控制器 的方法没有任何变化......它保持不变:
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.
Second, modify the jQuery click event:
Nothing changes in the Controller’s method…it stays the same:
您可以执行类似的操作,前提是您已为 ControllerName/EditReports/{ReportId} 配置了路由路径
You can do something like this, provided you have configured a route path for ControllerName/EditReports/{ReportId}