[英]Order List in MVC 4 ASP.NET i.e. Controller receiving 2 variables
I am trying to rewrite the default MVC Index controller that displays the list of records from a database. 我试图重写默认的MVC索引控制器,该控制器显示数据库中的记录列表。 I am trying to have an order by function on 1 of the headings AND the ability to search on the page:
我正在尝试按标题中的1个功能排序并在页面上进行搜索:
Controller code: 控制器代码:
public ActionResult Index(String sortOrder, String searchString) {
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
var profiles = from s in db.Profiles
select s;
if (!String.IsNullOrEmpty(searchString))
{
profiles = profiles.Where(d => d.lastName.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "Name_desc":
profiles = profiles.OrderByDescending(s => s.lastName);
break;
default:
profiles = profiles.OrderBy(s => s.lastName);
break;
}
return View(profiles.ToList());
}
View Code: 查看代码:
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder = "Name_desc" })
</th>
I have been trying various ways, but it seems as if I am not succeeding with the @html.actionLink calling the controller method 我一直在尝试各种方法,但是好像我没有成功使用@ html.actionLink调用控制器方法
Followed this tutorial exact but not working: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application 完全按照本教程操作,但不起作用: http : //www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-在asp-net-mvc应用程序中
So to explain how I solved the answer... 因此,为了解释我如何解决答案...
The actionResult was a HttpPost back. actionResult是一个HttpPost返回。 So it would only accept input when a button is clicked.
因此,仅在单击按钮时它将接受输入。 It seems as if @HTML.actionlinks only call the view methods.
似乎@ HTML.actionlinks仅调用视图方法。
I simply created a new Index(String sortOrder, String searchString)
method and the view code was @Html.ActionLink("Last Name", "/Index", new { sortOrder = ViewBag.NameSortParm})
我只是创建了一个新的
Index(String sortOrder, String searchString)
方法,视图代码为@Html.ActionLink("Last Name", "/Index", new { sortOrder = ViewBag.NameSortParm})
Its messy, but solved my issue nonetheless 它很杂乱,但仍然解决了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.