下拉列表值返回---选择---页面刷新后
问题描述:
因此,在我的应用程序中,用户将从下拉列表中选择一个名称,单击'查看',相应的值将显示在页面上。下拉列表值返回---选择---页面刷新后
然后使用超链接按升序对列表进行排序。为了发生这种情况,页面刷新并显示列表的新顺序。
下拉列表的值返回到其原始值'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)
任何帮助非常赞赏
答
你要绑定的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传递给操作。然后在返回之前,将其分配给模型。整个事情会像魔术一样工作。
任何帮助非常感谢?? – John
快速问题:你是否想将DropDownList设置为currentPersonID的值? – IyaTaisho