2016-02-12 55 views
3

我有一個操作從2 DropDownLists採取參數的操作家庭控制器。 我的目標是堅持從每個dropDownList中選擇的值,以便每當調用操作時定義兩個參數。 我的問題是,每次我選擇DropDownLists的任何一個新的值時,另一個的值被重置。控制器採取2下拉列表中的值的操作

控制器:

public ActionResult Results (int? PageSize,int? RaceId, int page = 0) 
     {      
      ///code that uses PageSize and RaceId to get data from DB    
     } 

public ActionResult _SelectRaces(int id=3) 
     {   
      var results = db.Races.Where(r => r.Event_Id == id); 
      if (results == null) 
      { 
       return HttpNotFound(); 
      } 
      List<SelectListItem> items = new List<SelectListItem>(); 
      foreach (var r in results.ToList()) 
      { 
       items.Add(new SelectListItem { Text = r.Name, Value = r.id.ToString() }); 
      } 

      ViewData["Race"] = items; 
      return PartialView(results.ToList()); 
     } 

public ActionResult _SelectPageSize() 
     { 
      List<SelectListItem> items = new List<SelectListItem>(); 
      items.Add(new SelectListItem { Text = "5", Value = "5" }); 
      items.Add(new SelectListItem { Text = "10", Value = "10" }); 
      items.Add(new SelectListItem { Text = "25", Value = "25" }); 

      ViewData["PageSize"] = items; 
      return PartialView(items); 
     } 

查看:

_SelectRaces.cshtml

@using (Html.BeginForm()) 
{ 
    <fieldset> 
     Select Race 
     @Html.DropDownList("RaceId", ViewData["Race"] as List<SelectListItem>, new { style = "width: 150px;", onchange = "this.form.submit()" }) 
    </fieldset> 
} 

_SelectPAgeSize.cshtml

@using (Html.BeginForm()) 
{ 
    <fieldset> 
     Select Page Size 
     @Html.DropDownList("PageSize", ViewData["PageSize"] as List<SelectListItem>, new { style = "width: 50px;", onchange = "this.form.submit()" }) 
    </fieldset> 
    ViewBag.PAge = "Page"; 
} 
在控制器

IndexViewModel.cs

public class IndexViewModel 
{ 
    public IList<SelectListItem> Races { get; set; } 
    public int raceSelected { get; set; } 
    public IList<SelectListItem> PageSize { get; set; } 
    public int pageSizeSelected { get; set; } 
} 

Index.cshtml

@Html.Action("_SelectPageSize")<!--Select Page Size--> 
    @Html.Action("_SelectRaces") <!--Select Race--> 
    @Html.Action("Results") 
+0

使用AJAX這個 –

+0

有點不清楚你想要什麼實現。您沒有顯示POST方法。我假設你想重定向到'Results'並將'PageSize'和'RaceId'傳遞給它?如果要保留這些值,請使用具有這些值屬性的視圖模型,並在調用Html時將視圖模型返回到「Index.cshtml」方法並將其傳遞給「_SelectPageSize」和「_SelectRaces」方法。行動()'。然後爲其他屬性生成一個隱藏的輸入。 –

回答

0

在這種情況下,我使用的視圖模型和局部視圖:

public ActionResult Index() 
    { 
     var results = db.Races.Where(r => r.Event_Id == id); 
     if (results == null) 
     { 
      return HttpNotFound(); 
     } 
     var items = new List<SelectListItem>(); 
     foreach (var r in results.ToList()) 
     { 
      items.Add(new SelectListItem { Text = r.Name, Value = r.id.ToString() }); 
     } 
     var pageSizes = new List<SelectListItem>(); 
     pageSizes.Add(new SelectListItem { Text = "5", Value = "5" }); 
     pageSizes.Add(new SelectListItem { Text = "10", Value = "10" }); 
     pageSizes.Add(new SelectListItem { Text = "25", Value = "25" }); 

     var model = new IndexViewModel() 
     { 
      Races = items, 
      PageSize = pageSizes 
     }; 
     return View(model); 
    } 

在指數:

@this.model IndexViewModel 
    @{ Html.RenderPartial("_SelectPageSize", this.Model); }<!--Select Page Size--> 
    @{ Html.RenderPartial("_SelectRaces", this.Model); } <!--Select Race--> 
    @{ Html.RenderPartial("Results", this.Model); } 

_SelectRaces.cshtml(例如)和_SelectPAgeSize:

@this.model IndexViewModel 

    <fieldset> 
      Select Race 
      @Html.DropDownListFor(x=>x.raceSelected, this.model.Races, new { style = "width: 150px;", onchange = "this.form.submit()" }) 
     </fieldset> 

在提交方法

public ActionResult Results (IndexViewModel model) 
      { 
var raceId = model.raceSelected; 
var pageSize = model.pageSizeSelected; 

       ///code that uses PageSize and RaceId to get data from DB    
      } 
相關問題