这让我疯了!
我有一个使用Ajax使用下面的脚本填充的视图。
function loadAcquisitions() {
var description = $("input#searchDescription").val();
var startDate = $("input#searchStartDate").val();
var endDate = $("input#searchEndDate").val();
$.ajax({
type: "POST",
dataType: "json",
url: "/Acquisition/Find",
data: { searchDescription: description, searchStartDate: startDate, searchEndDate: endDate },
success: function (jsonresults) {
$("tbody#acquisitionContent").find("tr").remove().end();
var pageCount = parseInt(jsonresults.length / pageSize);
var startItemIndex = currentPageIndex * pageSize;
var itemsToDisplay = pageSize + startItemIndex;
if ((jsonresults.length - startItemIndex) < pageSize) {
itemsToDisplay = jsonresults.length - startItemIndex + pageSize;
}
for (var i = startItemIndex; i < itemsToDisplay; i++) {
$("tbody#acquisitionContent").append('<tr>' +
'<td><a href="/Acquisition/Edit/' + jsonresults[i].AcquisitionID + '">Edit</a></td>' +
'<td>' + jsonresults[i].AcquisitionDescription + '</td>' +
'<td>' + jsonresults[i].AcquisitionDate + '</td>' +
'<td>$' + jsonresults[i].AcquisitionCost + '</td>' +
'<td>$' + jsonresults[i].InstallationCost + '</td>' +
'<td class="last">$' + jsonresults[i].DeliveryCost + '</td>' +
'</tr>');
}
$('#pageTitle').text('Page ' + (currentPageIndex + 1) + ' of ' + pageCount);
},
error: function (c, b, a) {
alert(a);
}
});
}
请注意,该链接是使用以下行生成的:
'<td><a href="/Acquisition/Edit/' + jsonresults[i].AcquisitionID + '">Edit</a></td>'
链接标记似乎是正确生成的,其格式为
/Acquisition/Edit/123
在控制器中,我有以下函数'Edit',如下所示:
public ActionResult Edit(int acquisitionID)
{
Acquisition acquisition = Acquisition.Get(acquisitionID);
return View(acquisition);
}
然后,在global.asax中,我有一个为“Edit”定义的路由,如下所示:
routes.MapRoute(
"EditAcquisition",
"{controller}/{action}/{acquisitionID}",
new { controller = "Acquisition", action = "Edit"}
);
我已经尝试更改控制器中的'Edit'函数以接受可以为空的int(int?)acquireID来消除错误 - 但不会改变将null传递给控制器的事实'从Ajax代码生成的链接编辑'函数。
非常感谢任何帮助!
谢谢!
更新
当我更新global.asax并将'acquireID'添加为如下参数时:
routes.MapRoute(
"EditAcquisition",
"{controller}/{action}/{acquisitionID}",
new { controller = "Acquisition", action = "Edit", acquisitionID = 0 }
);
应用程序的行为方式非常不同,因为导航链接现在都附加了“索引”。
/Acquisition/
变为
/Acquisition/Index
更新
这是global.asax的RegisterRoutes方法
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"EditProperty",
"{controller}/{action}/{propertyID}",
new {controller = "Property", action = "Edit"}
);
routes.MapRoute(
"EditAssignee",
"{controller}/{action}/{assigneeID}",
new {controller = "Assignee", action = "Edit"}
);
routes.MapRoute(
"EditAcquisition",
"{controller}/{action}/{acquisitionID}",
new {controller = "Acquisition", action = "Edit"}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
答案 0 :(得分:1)
对于我的路线,我始终包含URLParameter
设置:
routes.MapRoute(
"EditAcquisition",
"{controller}/{action}/{acquisitionID}",
new
{
controller = "Acquisition",
action = "Edit",
acquisitionID = UrlParameter.Optional
}
);
即使参数不可为空也适用。我也做了
acquisitionID = 0
我担心我无法解释它为什么会起作用。
修改强>
要尝试的其他事情
"EditAcquisition",
"Acquisition/Edit/{acquisitionID}",
new
{
controller = "Acquisition",
action = "Edit",
acquisitionID = UrlParameter.Optional
}
因此,URL与第二个参数匹配,而不是第三个参数,这是默认值。
答案 1 :(得分:1)
您的路线
routes.MapRoute(
"EditProperty",
"{controller}/{action}/{propertyID}",
new {controller = "Property", action = "Edit"}
);
匹配其他所有路线..(因为它们都具有相同的/controller/action/id
模式)
new {controller = "Property", action = "Edit"}
提供的值是默认值,而不是允许的值。
因此,/somecontroller/someaction/someid
形式的任何网址都会与之匹配,并且该ID将被称为propertyID
。
这就是您没有收到acquisitionID
要么只保留一条路线,请将propertyID
命名为id
之类的通用名称,并在方法中使用该路线,或者对所有具有相同模式的路线使用constraints
< / p>
routes.MapRoute(
"EditProperty",
"{controller}/{action}/{propertyID}",
new {controller = "Property", action = "Edit"},
new {controller = "Property", action="Edit"} // constraint
);