我已经创建了一个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");
}在Controller的方法中放置断点时,它会被击中,参数“reportId”将正确地传递给…但是代码的其余部分(返回视图()部分)似乎无法工作,因为在jQuery单击事件中,我有一个“返回false”。
当删除单击事件中的“返回假”时,断点不再被击中。因此,我无法访问“EditReport”视图…
我在这里遗漏了什么/不理解什么?
也就是…是否有更好/更好/更干净的方法来完成我的任务而不必使用AJAX调用?
发布于 2011-10-21 11:58:09
好的,因为我现在可以在这里发布答案(如果有人感兴趣的话):
首先,我需要修改ActionLink,并为参数输入一个预定义的值,该参数将在单击链接后被替换。
<%=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");
}发布于 2011-10-20 20:13:40
您可以这样做,只要您已经为ControllerName/EditReports/{ReportId}配置了路由路径
$("#edit").click(function() {
window.location.href = '<%=Url.Action("EditReport")%>' +'/'+ $('select[name="ReportId"] option:selected').val();
return false;
});https://stackoverflow.com/questions/7840291
复制相似问题