C# 对表进行排序/搜索不起作用
排序和搜索机制在我们的.NETMVC项目中不起作用。 我完成了控制器和视图的编码,除了排序和搜索之外,一切都很好 控制器:C# 对表进行排序/搜索不起作用,c#,asp.net-mvc,sorting,search,C#,Asp.net Mvc,Sorting,Search,排序和搜索机制在我们的.NETMVC项目中不起作用。 我完成了控制器和视图的编码,除了排序和搜索之外,一切都很好 控制器: public ActionResult Index(string sortOrder, string searchString) { ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.CustomerNameSort
public ActionResult Index(string sortOrder, string searchString)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.CustomerNameSortParm = String.IsNullOrEmpty(sortOrder) ? "cust_name_desc" : "";
ViewBag.LastSucceedOnSortParm = sortOrder == "Date" ? "succeed_date_desc" : "Date";
var farms = from f in db.Farms
select f;
if (!String.IsNullOrEmpty(searchString))
{
farms = farms.Where(s => s.LicenseKey.ToUpper().Contains(searchString.ToUpper()) || s.Name.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "name_desc":
farms = farms.OrderByDescending(f => f.Name);
break;
case "cust_name_desc":
farms = farms.OrderByDescending(f => f.CustomerName);
break;
case "succeed_date_desc":
farms = farms.OrderBy(f => f.LastSucceedOn);
break;
}
return View(db.Farms.ToList());
}
@using (Html.BeginForm())
{
<p>
Search: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.LicenseKey)
</th>
<th>
@*@Html.DisplayNameFor(model => model.Name)*@
@Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
@*@Html.DisplayNameFor(model => model.CustomerName)*@
@Html.ActionLink("CustomerName", "Index", new { sortOrder = ViewBag.CustomerNameSortParm })
</th>
<th>
@Html.DisplayNameFor(model => model.FarmDate)
</th>
<th>
@*@Html.DisplayNameFor(model => model.LastSucceedOn)*@
@Html.ActionLink("LastSucceedOn", "Index", new { sortOrder = ViewBag.LastSucceedOnSortParm })
</th>
<th></th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink(item.LicenseKey, "FarmDetails", "Farm", new { id = item.Id }, null)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FarmDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastSucceedOn)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id })
</td>
<td>
@Html.ActionLink("Settings", "Index", "Settings")
</td>
</tr>
}
</table>
查看:
public ActionResult Index(string sortOrder, string searchString)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.CustomerNameSortParm = String.IsNullOrEmpty(sortOrder) ? "cust_name_desc" : "";
ViewBag.LastSucceedOnSortParm = sortOrder == "Date" ? "succeed_date_desc" : "Date";
var farms = from f in db.Farms
select f;
if (!String.IsNullOrEmpty(searchString))
{
farms = farms.Where(s => s.LicenseKey.ToUpper().Contains(searchString.ToUpper()) || s.Name.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "name_desc":
farms = farms.OrderByDescending(f => f.Name);
break;
case "cust_name_desc":
farms = farms.OrderByDescending(f => f.CustomerName);
break;
case "succeed_date_desc":
farms = farms.OrderBy(f => f.LastSucceedOn);
break;
}
return View(db.Farms.ToList());
}
@using (Html.BeginForm())
{
<p>
Search: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.LicenseKey)
</th>
<th>
@*@Html.DisplayNameFor(model => model.Name)*@
@Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
@*@Html.DisplayNameFor(model => model.CustomerName)*@
@Html.ActionLink("CustomerName", "Index", new { sortOrder = ViewBag.CustomerNameSortParm })
</th>
<th>
@Html.DisplayNameFor(model => model.FarmDate)
</th>
<th>
@*@Html.DisplayNameFor(model => model.LastSucceedOn)*@
@Html.ActionLink("LastSucceedOn", "Index", new { sortOrder = ViewBag.LastSucceedOnSortParm })
</th>
<th></th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink(item.LicenseKey, "FarmDetails", "Farm", new { id = item.Id }, null)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FarmDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastSucceedOn)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id })
</td>
<td>
@Html.ActionLink("Settings", "Index", "Settings")
</td>
</tr>
}
</table>
@使用(Html.BeginForm())
{
搜索:@Html.TextBox(“搜索字符串”)
}
@DisplayNameFor(model=>model.LicenseKey)
@*@DisplayNameFor(model=>model.Name)*@
@ActionLink(“Name”,“Index”,new{sortOrder=ViewBag.NameSortParm})
@*@DisplayNameFor(model=>model.CustomerName)*@
@ActionLink(“CustomerName”,“Index”,new{sortOrder=ViewBag.CustomerNameSortParm})
@Html.DisplayNameFor(model=>model.FarmDate)
@*@DisplayNameFor(model=>model.LastSucceedOn)*@
@ActionLink(“LastSucceedOn”,“Index”,new{sortOrder=ViewBag.LastSucceedOnSortParm})
@foreach(模型中的var项目){
@ActionLink(item.LicenseKey,“FarmDetails”,“Farm”,new{id=item.id},null)
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.CustomerName)
@DisplayFor(modelItem=>item.FarmDate)
@DisplayFor(modelItem=>item.LastSucceedOn)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})
@ActionLink(“设置”、“索引”、“设置”)
}
我的代码哪部分是错的?一定有个简单的错误,但我看不出来。
谢谢。我理解db。农场是完整的列表。您可以对该列表进行排序和筛选,并将结果保存在本地农场变量中。但将未过滤的db.Farms返回给客户端。只需将其替换为农场,它就可以工作了
return View(farms.ToList());
执行所有排序,然后忽略所有排序,通过执行返回视图(db.Farms.ToList())再次调用数据库代码>-它需要
返回视图(farms.ToList())代码>啊。我知道我犯了一个愚蠢的错误。谢谢你展示。