Checkboxlist in asp.net MVC?

Yes I would also suggest an enum in this scenario Here is how you can do it in ASP. NET MVC: Your enum should look like this (see the link you provided): Flags public enum Days { Sunday = 0x1, Monday = 0x2, Tuesday = 0x4, Wednesday = 0x8, Thursday = 0x10, Friday = 0x20, Saturday = 0x40 } For reusability purposes I have created an generic html helper that looks like this: public static IHtmlString CheckboxListForEnum(this HtmlHelper html, string name, T modelItems) where T : struct { StringBuilder sb = new StringBuilder(); foreach (T item in Enum. GetValues(typeof(T)).Cast()) { TagBuilder builder = new TagBuilder("input"); long targetValue = Convert.

ToInt64(item); long flagValue = Convert. ToInt64(modelItems); if ((targetValue & flagValue) == targetValue) builder. MergeAttribute("checked", "checked"); builder.

MergeAttribute("type", "checkbox"); builder. MergeAttribute("value", item.ToString()); builder. MergeAttribute("name", name); builder.

InnerHtml = item.ToString(); sb. Append(builder. ToString(TagRenderMode.

Normal)); } return new HtmlString(sb.ToString()); } You can use the same html helper for all enumeration types Usage: Now for demonstration purposes let say that you have a model like this: Model: public class TVShow { public string Title { get; set; } public string Description { get; set; } public Days AvailableOn { get; set; } } Controller Action: public ActionResult Show() { var show = new TVShow { Title = "Late Late Show", AvailableOn = Days. Monday | Days. Friday }; return View(show); } View (strongly typed): %: Model.

Title %> ("days", Model. AvailableOn)%.

Yes I would also suggest an enum in this scenario. Here is how you can do it in ASP. NET MVC: Your enum should look like this (see the link you provided): Flags public enum Days { Sunday = 0x1, Monday = 0x2, Tuesday = 0x4, Wednesday = 0x8, Thursday = 0x10, Friday = 0x20, Saturday = 0x40 } For reusability purposes I have created an generic html helper that looks like this: public static IHtmlString CheckboxListForEnum(this HtmlHelper html, string name, T modelItems) where T : struct { StringBuilder sb = new StringBuilder(); foreach (T item in Enum.

GetValues(typeof(T)).Cast()) { TagBuilder builder = new TagBuilder("input"); long targetValue = Convert. ToInt64(item); long flagValue = Convert. ToInt64(modelItems); if ((targetValue & flagValue) == targetValue) builder.

MergeAttribute("checked", "checked"); builder. MergeAttribute("type", "checkbox"); builder. MergeAttribute("value", item.ToString()); builder.

MergeAttribute("name", name); builder. InnerHtml = item.ToString(); sb. Append(builder.

ToString(TagRenderMode. Normal)); } return new HtmlString(sb.ToString()); } You can use the same html helper for all enumeration types. Usage: Now for demonstration purposes let say that you have a model like this: Model: public class TVShow { public string Title { get; set; } public string Description { get; set; } public Days AvailableOn { get; set; } } Controller Action: public ActionResult Show() { var show = new TVShow { Title = "Late Late Show", AvailableOn = Days.

Monday | Days. Friday }; return View(show); } View (strongly typed): ("days", Model. AvailableOn)%.

I am not sure If we can create htmlhelpers in MVC2. I have created in MVC3. Please suggest.

Thanks – DotnetSparrow Jun 2 at 19:28 Yes it will work. I've created the same in MVC2. If you are going to use it in MVC 3 web app using Razor view engine, the only thing that will change is the view because Razor view engine has a different syntax.

– SpartanBeg Jun 2 at 19:33 It looks very good, however, on postback this does not respect the combining of enum values.Eg. If you were to check the 'Sunday' checkbox only that would show selected the next time you view? Anyway to fix this or am I missing something?

– sbeskur Jul 27 at 21:40.

I've got search form, which contains checkboxlist which is binded to my model. It's expecting, 0. How can I fix,edit form get action?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions