Vli*_*nce 4 model-view-controller asp.net-mvc
我已经创建了一个ASP.NET MVC 2.0应用程序.
我有一个带有"报告"列表的下拉框.在下拉列表旁边,我有两个ActionLink.一个说" 添加新报告 ",另一个说" 编辑报告 ".
" 添加新报告 "链接非常简单......它在我的Controller中调用ViewResult并返回一个new View()
.大!这没问题!
" 编辑报告 "链接有点棘手,因为我希望将下拉列表中当前所选项目的选定ID传递给ActionLink.
我发现了一篇文章,告诉我如何AJAX化我的ActionLink,但我做错了...
以下是"编辑"链接视图中的ActionLink:
<%=Html.ActionLink("Edit report", "EditReport", "Report", null, new { id = "edit" })%>
Run Code Online (Sandbox Code Playgroud)
这是用于处理"点击"的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;
});
Run Code Online (Sandbox Code Playgroud)
这是Controller中的方法:
public ViewResult EditReport(int reportId)
{
return View("EditReport");
}
Run Code Online (Sandbox Code Playgroud)
在Controller的方法中放置一个断点时,它会被命中并且参数" reportId "被正确传递...但其余代码(返回View()部分)似乎不起作用,因为在jQuery click事件中,我有一个"回归虚假".
在click事件中删除"return false"时,断点不再受到攻击.因此,我不能去我的"EditReport"视图......
我在这里缺少什么/不理解?
另外......有没有更好/更好/更清洁的方法来实现我的任务而无需使用AJAX调用?
Vli*_*nce 18
好的,因为我现在可以在这里发布答案(如果有人有兴趣):
首先,我需要修改ActionLink并为参数添加预定义值,一旦单击链接,该参数将被替换.
<%=Html.ActionLink("Edit report", "EditReport", "Report", new { reportId="xxx"}, new { id = "edit" })%>
Run Code Online (Sandbox Code Playgroud)
其次,修改jQuery click事件:
$("#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);
});
Run Code Online (Sandbox Code Playgroud)
Controller的方法没有任何变化......它保持不变:
public ViewResult EditReport(int reportId)
{
//Fix me...
return View("EditReport");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18693 次 |
最近记录: |