【问题标题】:Passing same SelectList with different SelectedValues to View?将具有不同 SelectedValues 的相同 SelectList 传递给 View?
【发布时间】:2017-12-07 10:59:17
【问题描述】:

我有一个包含两个下拉列表的视图,周末第 1 天和周末第 2 天。我处于编辑模式,并且从操作中我传递了我的选择列表,如下所示:

var WeekList1 = new SelectList(new List<SelectListItem>
{
     new SelectListItem { Value = "", Text = "Select Day" },
     new SelectListItem { Value = "1", Text = "Sunday" },
     new SelectListItem { Value = "2", Text = "Monday" },
     new SelectListItem { Value = "3", Text = "Tuesday" },
     new SelectListItem { Value = "4", Text = "Wednesday" },
     new SelectListItem { Value = "5", Text = "Thursday" },
     new SelectListItem { Value = "6", Text = "Friday" },
     new SelectListItem { Value = "7", Text = "Saturday" }
 }, "Value", "Text", model.weekendDay1);


var WeekList2 = new SelectList(new List<SelectListItem>
{
     new SelectListItem { Value = "", Text = "Select Day" },
     new SelectListItem { Value = "1", Text = "Sunday" },
     new SelectListItem { Value = "2", Text = "Monday" },
     new SelectListItem { Value = "3", Text = "Tuesday" },
     new SelectListItem { Value = "4", Text = "Wednesday" },
     new SelectListItem { Value = "5", Text = "Thursday" },
     new SelectListItem { Value = "6", Text = "Friday" },
     new SelectListItem { Value = "7", Text = "Saturday" }
 }, "Value", "Text", model.weekendDay2);

ViewBag.WeekEnd1 = WeekList1;
ViewBag.WeekEnd2 = WeekList2;

我两次创建相同的列表只是为了传递不同的选定值。有没有办法只创建一次并使用不同的选定值传递它。我不想为此创建新的数据库表。

更新:

@Html.DropDownList("WeekEnd1", null,  htmlAttributes: new { @class = "form-control" }) 

【问题讨论】:

  • 我已经更新了我的问题。请看一看。
  • 第一种方式会选择值本身吗?你能在答案部分给出解决方案吗?

标签: asp.net-mvc-4 viewbag selectlist


【解决方案1】:

型号:

public class CustomModel
{
     public string Week1 { get; set; }
     public string Week2 { get; set; }
     public SelectList Items { get; set; }
}

控制器:

public ActionResult Index()
{
     CustomModel model = new CustomModel();         
     model.Items = new SelectList(new List<SelectListItem>
                            {
                                 new SelectListItem { Value = "", Text = "Select Day" },
                                 new SelectListItem { Value = "1", Text = "Sunday" },
                                 new SelectListItem { Value = "2", Text = "Monday" },
                                 new SelectListItem { Value = "3", Text = "Tuesday" },
                                 new SelectListItem { Value = "4", Text = "Wednesday" },
                                 new SelectListItem { Value = "5", Text = "Thursday" },
                                 new SelectListItem { Value = "6", Text = "Friday" },
                                 new SelectListItem { Value = "7", Text = "Saturday" }
                             }, "Value", "Text");
     return View(model);
}

查看:

@using (Html.BeginForm())
{
     @Html.DropDownListFor(model => model.Week1, Model.Items)
     @Html.DropDownListFor(model => model.Week2, Model.Items)
     <input type="submit" value="SUBMIT" />
}

对于post方法中的这种方法,您可以根据模型属性获取值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多