Search code examples
asp.net-mvc-4viewbagselectlist

Passing same SelectList with different SelectedValues to View?


I have a view with two drop-downs, weekend Day 1 and weekend Day 2. I am in edit mode and from action I am passing my select list like this:

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;

I am creating same list twice just to pass different selected values. Is there any way to create it only once and pass it with different selected values. I do not want to crate new database table for this one.

Update:

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

Solution

  • Model:

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

    Controller:

    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);
    }
    

    View:

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

    For this approach in post method, you can get the values based on model property.