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)
任何幫助非常讚賞
任何幫助非常感謝?? – John
快速問題:你是否想將DropDownList設置爲currentPersonID的值? – IyaTaisho