Looks like everyday challenges are keeping this blog active…
. Here again, is a new requirement which led to workarounds from the suggestions/solution proposed my many on the net.
The need
Accepting courses held on various days of the week and storing the same in a single field in database table, of course, as a string. And later allowing the user to edit the stored values and saving the new selection back to the table.
While binding, we bind only to one field in the model, but need to display multiple checkboxes. A string value (stored as ‘0101010’, for the above selection) where every digit represents the selection value of that day, 0 for false and 1 for true. Thankfully, there are only seven days in a week…
.
The solution
Could have been a ListBox or CheckBoxList type, but that would show the options in a box and top down. For days of the week, a horizontal selection makes the interface more appealing and friendly.
So it had to be a HTML helper extension. A class and function to be created to for this, as below,
namespace Project1.Helpers
{
public static class CheckBoxHelper
{
public static MvcHtmlString DoWCheckBox(this HtmlHelper html, Expression<Func> expression)
{
This HtmlHelper extension is invoked in the view (Edit.cshtml) as below,
@model Project1.Models.COURSE_MST
@using Project1.Helpers;
The extension function parameter expression would hold the ‘model.Held_On’, and parameter html would have the values, i.e, ‘0101010’ in the above case. Only the Edit part is shown here, something similar needs to be done for Create of the controller.
public static MvcHtmlString DoWCheckBox(this HtmlHelper html, Expression<Func> expression)
{
// get the name of the property, split ‘model.Held_On’
string[] propertyName = expression.Body.ToString().Split(‘.’);
string[] wod = {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”};
// get the value of the property
Func compile = expression.Compile();
string strValue = compile(html.ViewData.Model);
var sb = new StringBuilder();
for (int i = 0; i < strValue.Length; i++)
{
TagBuilder checkbox = new TagBuilder(“input”);
checkbox.MergeAttribute(“id”, propertyName[1]);
checkbox.MergeAttribute(“name”, propertyName[1]);
checkbox.MergeAttribute(“type”, “checkbox”);
checkbox.MergeAttribute(“value”, Convert.ToString(i));
if (strValue.Substring(i,1) == “1″)
checkbox.MergeAttribute(“checked”, “checked”);
TagBuilder label = new TagBuilder(“label”);
label.MergeAttribute(“for”, wod[i]);
label.SetInnerText(wod[i]);
sb.Append(checkbox);
sb.Append(label);
}
return MvcHtmlString.Create(sb.ToString());
}
As you would have noticed, we are not using hidden type html tags to store the value, but storing integer variable ‘i’ value (checkbox.MergeAttribute(“value”, Convert.ToString(i))).
This would generate the required list of selected days of week and display with the same id and name, thus allowing us to get the new selection under one variable. To capture the new selection we would modify the Edit (post) as below,
[HttpPost]
public ActionResult Edit(COURSE_MST course_mst, int[] Held_On)
{
As this checkbox value stores an integer, a int array is the additional parameter to the function (note the variable values in the watch window). The array would contain the values only where the checkboxes have been selected, values for the non-selected checkboxes is not sent to the controller.
The values in this example contain ’1,6′ representing Mon and Sat, which translates to ’0100001′…
Take charge.

