MVC MultiSelect Control for Day of week

1.5k views Asked by At

I have a ASP.NET MVC 4 project with EF and I have a table Orders with a field ShipmentDay varchar(7)/string where I want to save in the database in witch days of the week the order can be shipped : 1000100 for Monday and Friday.

Right now I have a ListBox :

@Html.ListBoxFor(model => model.ShipmentDay, new MultiSelectList(ViewBag.ShipmentDay))

List<string> lbSDay = new List<string>();
ZileLiv.Add("Monday"); ...

public ActionResult Create()
{
    ViewBag.lbSDay = lbSDay ;
    return View();
}

Q: How can I make a custom multiselect control to select the days of week ?

1

There are 1 answers

0
pswillies On BEST ANSWER

I'd start out by getting rid of the varchar(7) and storing the shipment days in a single byte with the following weekday values...

private const byte MONDAY = 1;
private const byte TUESDAY = 2;
...
private const byte SUNDAY = 64;

Create a html helper extension method to build up a multiselect using those values

to give, e.g.

<select multiple>
    <option value=1>Monday</option>
    ...
    <option value=64>Sunday</option>
</select>

Calculate if the option should be selected using something like

bool checked = ((byte)(shipDays & MONDAY) == MONDAY);