2

因此,在我的應用程序中,用戶將從下拉列表中選擇一個名稱,單擊'查看',相應的值將顯示在頁面上。下拉列表值返回---選擇---頁面刷新後

然後使用超鏈接按升序對列表進行排序。爲了發生這種情況,頁面刷新並顯示列表的新順序。

下拉列表的值返回到其原始值'select',而不是保留所選人員的姓名。

我的模型:

public class HolidayList 
    { 
     public List<Holiday> HList4DD { get; set; } 
     public List<Person> PList4DD { get; set; } 

     public int currentPersonID { get; set; } 
     public IEnumerable<SelectListItem> Categories { get; set; } 

     public HolidayList() 
     { 
      HList4DD = new List<Holiday>(); 
      PList4DD = new List<Person>(); 
      } 
     } 
    } 

我的控制器:

[HttpPost] 
     public ViewResult Index(int HolidayDate) 
     { 
      var holidays = db.Holidays.Include("Person"); 

      HolidayList model = new HolidayList(); 

      model.currentPersonID = HolidayDate; 
      model.PList4DD = db.People.ToList();   
      model.Categories = holidays.Select(x => new SelectListItem 
              { 
               Value = x.Id.ToString(), 
               Text = x.Person.Name 
              } 
             ); 


      int data = HolidayDate; 

      model.HList4DD = db.Holidays.Where(h => h.PersonId == HolidayDate).ToList();  

      return View(model); 

     } 

     [HttpGet] 
     public ViewResult Index(string sortOrder, int? currentPersonID) 
     { 
      var holidays = db.Holidays.Include("Person"); 

      HolidayList model = new HolidayList(); 

      //not null 
      if (currentPersonID.HasValue) 
      { 
       model.currentPersonID = currentPersonID.Value; 

      } 
      else 
      { 
       model.currentPersonID = 0; 
      } 

      model.PList4DD = db.People.ToList(); 

      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "date" : ""; 
      var dates = from d in db.Holidays 
         where d.PersonId == currentPersonID.Value 
         select d; 

      switch (sortOrder) 
      { 
       case "date": 
        dates = dates.OrderBy(p => p.HolidayDate); 
        break; 
      } 

      model.HList4DD = dates.ToList(); 

      return View(model); 
     } 

我看來

我已經嘗試了許多不同的在這裏嘗試的,下面的代碼工作但有下拉列表問題

@Html.DropDownListFor(model => model.HList4DD.First().HolidayDate, 
           new SelectList(Model.PList4DD, "Id", "Name"), 
           // Model.currentPersonID 
           "---Select---" 
           ) *@ 

我嘗試解決此是:

@Html.DropDownList("HolidayDate", Model.Categories, "---Select---") 

@Html.DropDownListFor("HolidayDate", x => x.HolidayDate, Model.Categories) 

任何幫助非常讚賞

+0

任何幫助非常感謝?? – John

+0

快速問題:你是否想將DropDownList設置爲currentPersonID的值? – IyaTaisho

回答

2

你要綁定的DropDownFor到錯誤的性質。 基本上你想要做的是在你的模型中,創建一個新的屬性來綁定下拉列表中選定的值。

public int SelectedDate {get;set;} 

然後在你的代碼前你想使用dropdownFor的屬性綁定這樣

@Html.DropDownListFor(model => model.SelectedDate , 
    new SelectList(Model.PList4DD, "Id", "Name"), 
    // Model.currentPersonID 
    "---Select---" 
) 

不是這個。

@Html.DropDownListFor(model => model.HList4DD.First().HolidayDate , 
    new SelectList(Model.PList4DD, "Id", "Name"), 
    // Model.currentPersonID 
    "---Select---" 
) 

Finnaly,在您想要進行排序的操作中,您需要將SelectedDate傳遞給操作。然後在返回之前,將其分配給模型。整個事情會像魔術一樣工作。

相關問題