【发布时间】:2014-01-28 07:35:47
【问题描述】:
我正在尝试按照此页面 (http://www.asp.net/mvc/tutorials/mvc-5/introduction/adding-search) 中有关如何创建带有网站下拉列表的搜索页面的说明进行操作。但似乎无法获得下拉列表。它一直给我这个错误:
System.ArgumentException:DbSortClause 表达式必须具有顺序可比的类型。参数名称:key
问题是:CountryList.AddRange(CountryQry.Distinct());
搜索控制器 :::
public ActionResult Index(string location, string searchString)
{
var CountryList = new List<Country>();
var CountryQry = from d in db.Stuffs
orderby d.Country
select d.Country;
CountryList.AddRange(CountryQry.Distinct());
ViewBag.location = new SelectList(CountryList);
var stuff = from m in db.Stuffs
select m;
if (!String.IsNullOrEmpty(searchString))
{
stuff = stuff.Where(s => s.stuffName.Contains(searchString));
}
if (!String.IsNullOrEmpty(location))
{
stuff = stuff.Where(x => x.Country.countryName == location);
}
return View(stuff);
}
查看 :::
<form>
@using (Html.BeginForm("Index","Search",FormMethod.Get)){
@Html.TextBox("SearchString", new { placeholder = "text" })
@Html.DropDownList("location", "All")
}
</form>
Model :::(这是从数据库自动生成的)
public partial class Stuff
{
public string stuffId { get; set; }
public string stuffName { get; set; }
public string countryId { get; set; }
public virtual Country Country { get; set; }
}
我的 c# 知识非常有限,因此,我希望有人可以帮助我。 任何帮助表示赞赏!谢谢!
【问题讨论】:
标签: c# drop-down-menu asp.net-mvc-5