将具有不同 SelectedValues 的相同 SelectList 传递给 View?
Passing same SelectList with different SelectedValues to View?
我有一个带有两个下拉列表的视图,周末第 1 天和周末第 2 天。我处于编辑模式,并且通过操作我正在传递我的 select 列表,如下所示:
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;
我创建同一个列表两次只是为了传递不同的 selected 值。有什么方法可以只创建一次并使用不同的 selected 值传递它。我不想为此创建新数据库 table。
更新:
@Html.DropDownList("WeekEnd1", null, htmlAttributes: new { @class = "form-control" })
型号:
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 方法中的这种方法,您可以根据模型 属性 获取值。
我有一个带有两个下拉列表的视图,周末第 1 天和周末第 2 天。我处于编辑模式,并且通过操作我正在传递我的 select 列表,如下所示:
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;
我创建同一个列表两次只是为了传递不同的 selected 值。有什么方法可以只创建一次并使用不同的 selected 值传递它。我不想为此创建新数据库 table。
更新:
@Html.DropDownList("WeekEnd1", null, htmlAttributes: new { @class = "form-control" })
型号:
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 方法中的这种方法,您可以根据模型 属性 获取值。