如何在分页时为SelectListItem设置viewbag以进行搜索?

时间:2015-01-30 04:57:30

标签: asp.net-mvc search html.dropdownlistfor

如何将viewbag设置为dropdownlist,就像我在代码中为@Html.Textbox设置的那样。因为当我通过PagedList

进入下一页时,我想保留dropdowlist的值
@using (Html.BeginForm("Index", "Account", FormMethod.Get))
{
  <div class="inDiv">
    <label>Filter data :</label>
  </div>
  <div class="inDiv">
    @Html.DropDownList("key", new List<SelectListItem>()
    {
      new SelectListItem{ Text="[ Theo kiểu ]"},
      new SelectListItem{Value="accountname", Text="Account Name"},
      new SelectListItem{Value="fullname", Text="Full Name"},
    }, new { @class = "form-control input-sm" })
  </div>
  <div class="inDiv">
    @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "form-control input-sm", @placeholder = "input here ..." })
  </div>
  <div class="inDiv">
    <input type="submit" class="btn btn-default btn-sm" value="Search">
  </div>
}

1 个答案:

答案 0 :(得分:0)

在Controller ActionMethod上

var Cities = new List<SelectListItem>();
Cities.Add(new SelectListItem { Name = "Nagpur", Id = "1" });
Cities.Add(new SelectListItem { Name = "Pune", Id = "2" });
Cities.Add(new SelectListItem { Name = "Mumbai", Id = "3" });
Cities.Add(new SelectListItem { Name = "Delhi", Id = "4" });

ViewBag.CityList = new SelectList(Cities, "Id", "Name");

观看

@Html.DropDownList("DispCity", ViewBag.CityList)