2

所以在我的应用程序中,用户将从下拉列表中选择一个名称,单击“查看”,相应的值将显示在页面上。

然后使用超链接按升序对列表进行排序。为此,页面会刷新并显示列表的新顺序。

下拉列表的值返回到其原始值“选择”,而不是保留所选人员的姓名。

我的模型:

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)

非常感谢任何帮助

4

1 回答 1

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---"
)  

最后,在您想要进行排序的操作中,您需要将 SelectedDate 传递给操作。然后在返回之前,将其分配给模型。整个事情会像魔术一样工作。

于 2012-12-06T23:41:06.540 回答