.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

CheckBoxList Html Helper

Posted By:      Posted Date: September 22, 2010    Points: 0   Category :ASP.Net


I have created a CheckBoxList and a CheckBoxListFor helpers that I am using as follows:

@Html.CheckBoxListFor(u => u.Roles, (MultiSelectList)View.Roles)

The checkboxes are being displayed but they are not being checked with the values from u.Roles. They are all unchecked.

My Html Helpers are as follows:

    public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<String, Object> htmlAttributes) where TModel : class {

      ModelMetadata metadata = ModelMetadata.FromLambdaBLOCKED EXPRESSION;

      String field = ExpressionHelper.GetExpressionText(expression);

      String name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(field);

      return CheckBoxList(htmlHelper, metadata.DisplayName ?? metadata.PropertyName ?? field, selectList, htmlAttributes == null ? new RouteValueDictionary() : new RouteValueDictionary(htmlAttributes));

    } // CheckBoxListFor

    public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, String name, IEnumerable<SelectListItem> selectList, 

View Complete Post

More Related Resource Links

Using Dynamic Property inside Html Helper with Razor fires an error



I have the following with Razor:

@Html.GoogleAjax(View.GoogleAjaxKey, new String[,] { { "jquery", "1.4.2" } })

And the I get a strange error:

  'System.Web.Mvc.HtmlHelper<object>' has no applicable method named 'GoogleAjax' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

What is strange is that it works fine using:

@Html.GoogleAjax("123", new String[,] { { "jquery", "1.4.2" } })

I tried the following code line:


And the key is displayed just fine so the problem is how I use the value of @View.GoogleAjaxKey into Html.GoogleAjax.

Do I need to change something on my Html Helper? Here it is:

    public static MvcHtmlString GoogleAjax(this HtmlHelper helper, String key, String[,] libraries) {


Html Helper Customization


I am new to this, and looking for best practices in this area.

What is a good way to customize extensions to standardize accross the whole website?

For example I have TextArea which I want to resize to look and feel the same across the website wherever I call it. And similarly I would like to use some sort of a date picker element.

Btw: In addition, I am unable to understand what the row and column arguments do in the Html.TextArea( ... int row, int col, ... ). They did not resize the textarea at all: Html.TextArea( ..., new { width='20', height='10' }  ). I also tried sending in the Height and width arguments in html attributes, but that doesn't seem to work either. Looking for advice in that as well.


Custom Strong type HTML Helper for checkboxes?



MVC contains a strong typed HTML Helper (HTML.CehckBoxFor()) this takes a bool and returns a bool. To make the URL smaller I am thinking to change the bool values on the model object to byte or somthing like that so the url only contains &Parameter=1.

I have found this snippet :

public static string DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
  where TModel : class
    var inputName = ExpressionHelper.GetExpressionText(expression);
    return htmlHelper.DatePicker(inputName);

But I have no clue how to turn this in to a strong typed HTML Helper for checkbox that takes byte instead of bool. Its also important that the model object is set with byte instead of bool.

Any sugestions?


Any HTML Helper Repositories?


Are there any websites with collections of HTML Helpers?  One thing that is holding me back with MVC is reinventing the wheel compared to WebForms.  Unfortunately, I don't have the time or skill to create complicated HTML Helpers such as editible data grid tables with sorting, calendars, sitemaps, logins, etc.


MVC 3 Beta. Google Analytics Html Helper.



I think (I read it somewhere and can't find the source again) there is a new Google Analytics Html Helper on MVC 3 Beta.

Where can I find the source for this helper?

Thank You,


create a html helper for tny mce htlm editor?


how can I create a html helper for tny mce htlm editor?

2 simple ways to create Custom Html Helper in ASP.NET MVC

ASP.NET MVC facilitates us to create our Custom Html Helper in following simple ways:

1. Creating Extension Method on Html Helper Class
2. Creating Static Methods

ASP.NET, HTML, JavaScript Snippet Support (VS 2010 and .NET 4.0 Series)

This post covers another useful improvement in VS 2010 - HTML/ASP.NET/JavaScript snippet support. Snippets allow you to be more productive within source view by allowing you to create chunks of code and markup that you can quickly apply and use in your application with a minimum of character typing.

Visual Studio has supported the concept of "snippets" for VB and C# in previous releases - but not for HTML, ASP.NET markup and JavaScript. With VS 2010 we now support snippets for these content types as well.

Maintain HTML Radio Button Selection Inside GridView After Postback

Long time ago I wrote an article about getting the value of the selected RadioButtons inside the GridView control. The code did not addresssed the postback issue which means that the selected radio button was cleared after the postback occurs.

Rendering ASP.NET Script References into the Html Header

One thing that I've come to appreciate in control development in ASP.NET that use JavaScript is the ability to have more control over script and script include placement than ASP.NET provides natively. Specifically in ASP.NET you can use either the ClientScriptManager or ScriptManager to embed scripts and script references into pages via code.

Getting value of Html.TextBoxFor(m => m.name)


Hi All,

I am new to Asp.net MVC 2. I am unable to get value for Html.TextBoxFor(m = > m.name) in view. I can get this value in Controller.

How to get this value in view.

Please help.

Embed Youtube inside Ajax Html Editor- problem



When inserting a simple embed of youtube inside the ajax editor, on firefox and chrome problems occur. On the other hand on internet explorer 8 everything works fine. I found this solution http://www.nopcommerce.com/boards/t/4228/bug-in-ajax-html-editor.aspx . 

My question is where do I find the DesignPanel.pre.js ?


Roderick Vella

Re-positioning an HTML container with javascript onresize with a Master page.


I have an HTML <div> container that I float to the right of a gridview.  When the browser window is maximized, it looks fine.  When minimized, the gridview positions below the floating container and most of the data is pushed off the bottom of the browser - you have to scroll down to see it.

If I position the floating container above the gridview it looks fine in a minimized window, but looks real bad when maximized.

What I want to do is use a javascript: onresize event to re-position the floating container up or down depending on the window.inner.width AND do it with a Master/Content page structure AND do it for only the one page where needed.

I can capture the onresize event and display the window dimensions but haven't figured-out how to do it with a Content page that still uses the Master page.  I think I'm having a very "thick" week...

- Tinker


adding a url to html img


I am having an html img tag in which the user is being shown images. However I now want to open a url when the user clicks my img. 
What do I need to do to be able to achieve this? 

VS 2010 HTML validation "in CSS" option - where the hell is it?


In VS 2008 there is a very useful feature of background HTML validation - it checks if CSS class name is valid i.e. it exists in one of the linked css files. But VS 2010 Express does not do that any more. And the relevant setting in Options->Text Editor->HTML->Validation->in CSS is not there. Ok, maybe they made it paid for option? VS 2010 Professional? Not there. Or maybe VS 2010 Ultimate? Not a trace.

1. am I blind?

2. they decided it is too convenient to use and removed it?

3. they will "introduce" it in SP1?

What is going on? That one thing made me move back to VS 2008 web developer.


VWD rearranging HTML


Hey guys,

Have been VWD for a while now but VWD2010 seems to have one really annoying habbit. I'll write a whole load HTML with server tags and when I save, not always but quite often, VWD will squash all tags together in seemingly random arrangement completely removing my formatting conventions that make it easy for me to read. All the tags are still in the right order and makes no difference to how the page works, it just makes it really difficult to read quickly when scanning over it looking for a particular section.

For example:

        <asp:Panel ID="pnlConfirm" runat="server" Visible="false">
<asp:Table ID="tblConfBilling" runat="server">
<asp:Label ID="lblName" runat="server" Text="" />
<asp:Label ID="lblAddress" runat="server" Text="" />

Encoding en Decoding HTML



I stored in a database a ntext "<b>Hello</b>";

But when  I read it from the database I see <b>Hello</b> instead of Hello.

In the html source I see this: &lt;b&gt;Hello&lt;/b&gt;

Any Idea?


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend