单击时无 Url 参数
No Url Parameter on click
我正在尝试按照本教程进行操作,但将其重新用于我自己的应用程序。
但是,按照教程中的说明单击标题时我遇到了一些问题,我的 url 没有更改为..
localhost:8000/Application?sortOrder=Date_desc
相反,我只得到以下内容
localhost:8000/应用程序
因此 sortOrder 始终为 null,并且排序会转移到开关中的默认项。
为什么会这样?据我所知,我已经遵循了教程。谁能解释我可能哪里出错了?
控制器
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParam = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
ViewBag.AppSSortParm = sortOrder == "AppS" ? "AppS_desc" : "AppS";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var applications = from a in db.applications select a;
if (!String.IsNullOrEmpty(searchString))
applications = applications.Where(s => s.applicationDescription.ToUpper().Contains(searchString.ToUpper()));
switch (sortOrder)
{
case "Name_desc":
applications = applications.OrderByDescending(s => s.applicationDescription);
break;
case "AppS_desc":
applications = applications.OrderByDescending(s => s.applicationWindowSecurity);
break;
case "AppS":
applications = applications.OrderBy(s => s.applicationWindowSecurity);
break;
default:
applications = applications.OrderBy(s => s.applicationDescription);
break;
}
int pageSize = 8;
int pageNumber = (page ?? 1);
return View(applications.ToPagedList(pageNumber, pageSize));
}
查看
<th>
@Html.ActionLink("App Description", "Index", new { sortOrder = ViewBag.NameSortParm }, htmlAttributes: new { @class = "control-label" })
</th>
<th>
@Html.ActionLink("App Sec Over", "Index")
</th>
<th>
@Html.ActionLink("App Window Sec", "Index", new { sortOrder = ViewBag.AppSSortParm }, htmlAttributes: new { @class = "control-label" })
</th>
您需要将 actionLink 中的参数与您在控制器 Action 方法中期望的参数相匹配。请注意,ViewBag 是一个动态对象,因此很容易犯这种错误(因为它在运行时而不是编译时检查成员)。
我正在尝试按照本教程进行操作,但将其重新用于我自己的应用程序。
但是,按照教程中的说明单击标题时我遇到了一些问题,我的 url 没有更改为..
localhost:8000/Application?sortOrder=Date_desc
相反,我只得到以下内容
localhost:8000/应用程序
因此 sortOrder 始终为 null,并且排序会转移到开关中的默认项。
为什么会这样?据我所知,我已经遵循了教程。谁能解释我可能哪里出错了?
控制器
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParam = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
ViewBag.AppSSortParm = sortOrder == "AppS" ? "AppS_desc" : "AppS";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var applications = from a in db.applications select a;
if (!String.IsNullOrEmpty(searchString))
applications = applications.Where(s => s.applicationDescription.ToUpper().Contains(searchString.ToUpper()));
switch (sortOrder)
{
case "Name_desc":
applications = applications.OrderByDescending(s => s.applicationDescription);
break;
case "AppS_desc":
applications = applications.OrderByDescending(s => s.applicationWindowSecurity);
break;
case "AppS":
applications = applications.OrderBy(s => s.applicationWindowSecurity);
break;
default:
applications = applications.OrderBy(s => s.applicationDescription);
break;
}
int pageSize = 8;
int pageNumber = (page ?? 1);
return View(applications.ToPagedList(pageNumber, pageSize));
}
查看
<th>
@Html.ActionLink("App Description", "Index", new { sortOrder = ViewBag.NameSortParm }, htmlAttributes: new { @class = "control-label" })
</th>
<th>
@Html.ActionLink("App Sec Over", "Index")
</th>
<th>
@Html.ActionLink("App Window Sec", "Index", new { sortOrder = ViewBag.AppSSortParm }, htmlAttributes: new { @class = "control-label" })
</th>
您需要将 actionLink 中的参数与您在控制器 Action 方法中期望的参数相匹配。请注意,ViewBag 是一个动态对象,因此很容易犯这种错误(因为它在运行时而不是编译时检查成员)。