我遇到路由问题。我正在使用下面的JavaScript函数向我的控制器StudentSearch发帖。当我发帖子时,它只传递了第一个参数。该页面为null,这是预期的;但是,开始和结束日期具有日期值,但两个参数都为空。我在这里错过了什么?我阅读了文档并在线搜索,到目前为止我还没有取得任何成功。
//Javascript function
findStudent function()
{
var SearchValue= $("#Search").val();
var StartDate = $('#StartDate').val();
var EndDate = $('#EndDate').val();
var url = '@Url.Action("Search")';
location.href = url + "/" + SearchValue + "/" + StartDate +"/" + EndDate+;
}
//This is the ActionResult in the controller
public ActionResult Search(string SearchValue, string StartDate , string EndDate)
{
//EndDate and STart Date are null here.
}
//the route section in my RouteConfig.cs file
routes.MapRoute(
name: "Search",
url: "StudentSearch/Search/{SearchValue}/{StartDate}/{EndDate}",
defaults: new { controller = "StudentSearch", action = "Search",
SearchValue = UrlParameter.Optional,StartDate=UrlParameter.Optional,
EndDate = UrlParameter.Optional}
);
下午02:50更新: 阅读Chris评论后,我修改了代码。它没有路由到StudentSearch控制器。我没找到404页面。正如你所看到的那样,我分别将Jane传入searchvalue,并将student1,student2分别用于开始和结束(这不是问题,因为参数预计是一个字符串)。 ... / StudentSearch /搜索/简/ student1 / STUDENT2
答案 0 :(得分:1)
不确定你在这里期待什么。 url
变量将解析为字符串'/StudentSearch/Search'
,因为您为所有路径参数传递空字符串。无论如何,这实际上毫无意义。只有@Url.Action("Search")
才会得到相同的结果。
然后将变量SearchValue
附加到此字符串,该字符串来自$('#Search').val()
。而且,就是这样。永远不会使用StartDate
和EndDate
,尤其不会在构建网址时使用。
然后您的路由本身不接受除SearchValue
之外的任何通配符字符串。即使您确实将StartDate
和EndDate
添加到了网址,一切都会进入SearchValue
参数,您的实际StartDate
和EndDate
动作参数仍然存在是空的。你的路线需要像:
url: "StudentSearch/Search/{SearchValue}/{StartDate}/{EndDate}",
正确填写所有参数。