私はアクション SearchPost を持っています。取得アクションで:
ViewBag.cities = db.cities.ToList();
ViewBag.areas = db.areas.ToList();
IEnumerable<SelectListItem> Months =pm.GetMyMonthList().Select(a => new
SelectListItem {
Value = a.Value,
Text = a.Text
}).ToList();
ViewBag.monthList = Months;
IEnumerable<SelectListItem> rentTypes = pm
.GetRentType()
.Select(a => new SelectListItem
{
Value = a.Value,
Text = a.Text
})
.ToList();
ViewBag.typeList = rentTypes;
return View(db.posts.Include("user").ToList());
ビューページで
@Html.DropDownList("City",new SelectList(ViewBag.cities as
System.Collections.IEnumerable, "city_id", "name"),
"Select City", new { id = "ddlCars" })
@Html.DropDownList("Area", new SelectList(Enumerable.Empty<SelectListItem>(),
"area_id", "name"),
"Select Area", new { id = "ddlModels" })
@Html.DropDownList("rent_type", new SelectList(ViewBag.typeList as
System.Collections.IEnumerable, "Value", "Text"), "Select Category")
@Html.DropDownList("month", new SelectList(ViewBag.monthList as
System.Collections.IEnumerable, "Value", "Text"), "Select Month")
私の投稿アクションで:
IList<post> p = db.posts.Include("user").ToList();
string Area = Request.Form["Area"];
string City = Request.Form["City"];
if (City != "Select City" && City != null && City !="")
{
int c_id = Convert.ToInt32(City);
city c = db.cities.Single(s => s.city_id == c_id);
ci = c.name;
}
if (Area != "Select Area" && Area != null && Area !="")
{
int a_id = Convert.ToInt32(Area);
area a = db.areas.Single(x => x.area_id == a_id);
ar = a.name;
}
string rent_type = Request.Form["rent_type"];
string month = Request.Form["month"];
if (rent_type != null && rent_type != "")
{
if ((p != null) && (p.Any()))
{
p = p.Where(a => a.rent_type == rent_type).ToList();
}
}
if (month!= null && month != "")
{
if ((p != null) && (p.Any()))
{
p = p.Where(a => a.rent_month == month).ToList();
}
}
return View(p);
ここでは、最初にデータベースからすべての投稿を選択しました。次に、都市、地域、月、家賃の種類でフィルタリングしたいと思います。都市と地域はカスケード ドロップダウン リストです。都市と地域を選択すると、レコードが正しくフィルター処理されます。しかし、都市のみを選択するとエラーが発生します:
シーケンスには要素が含まれていません行で:
エリア a = db.areas.Single(x => x.area_id == a_id);月またはrent_typeのみでフィルタリングしようとすると、レコードがフィルタリングされず、常にレコードが表示されません。
これら2つの問題を解決するには、コードのどの部分を変更すればよいですか...前もって感謝します...