Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


bool editor template in mvc4

Posted on 2014-01-24
Medium Priority
Last Modified: 2014-03-06
I am trying to create an Editor Template for bool types in MVC4.  I successfully created one for string types but can't get the one for boolean types working.  Here is what I have so far:

Under Views -> Shared -> Editor Templates, I have the following ascx file called bool.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<bool>" %>
<%   var htmlAttributes = new ENL.HtmlPropertiesAttribute();   
         htmlAttributes = (ENL.HtmlPropertiesAttribute) ViewData.ModelMetadata.AdditionalValues["HtmlAttributes"];   
     htmlAttributes.HtmlAttributes().Add("class", "text-box single-line " + htmlAttributes.CssClass); 
    <%= Html.Label(ViewData.ModelMetadata.DisplayName, new { @class = "editor-label" })%> :        
    <%= Html.CheckBox(String.Empty, ViewData.TemplateInfo.FormattedModelValue)%>                                         

Open in new window

In my ENL_GroupNamesMetadata.cs class I have the following fields as part of my model:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
internal sealed class ENL_GroupNamesMetadata
    [DisplayName("Group Name ID")]
    public int ENLGroupNameID { get; set; }
    [ENL.HtmlProperties(Size = 40, MaxLength = 30)]
    [Required(ErrorMessage = "* Group Name is required")]
    [DisplayName("Group Name")]
    public string ENLGroupName { get; set; }
//    [UIHint("bool")]
    public bool IsActive { get; set; }

Open in new window

Here is the markup in my Edit.cshtml:
        <div class="editor-field">
            @Html.EditorFor(model => model.IsActive)
            @Html.ValidationMessageFor(model => model.IsActive)

Open in new window

Here are the problems I have:

1. The IsActive property is not being recognized as a bool and displayed using the bool.ascx markup.  The checkbox is displayed and is checked but the label isn't displayed.
2. If I add [UIHint("bool")] to the IsActive property, the label is displayed along with the checkbox but the checkbox isn't checked.

 If I put ViewData.TemplateInfo.FormattedModelValue in a TextBox, true is displayed.

Any help with this is greatly appreciated!
Question by:dyarosh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 39808690
I am confused...

Did you try this?

@Html.CheckBoxFor(model => model.IsActive)

Author Comment

ID: 39808726
I can't use it because it is a template.  I can't access the model directly.
LVL 96

Expert Comment

by:Bob Learned
ID: 39809049
You have the model here:

<div class="editor-field">
            @Html.EditorFor(model => model.IsActive)
            @Html.ValidationMessageFor(model => model.IsActive)

Open in new window

I meant that you could use @Html.CheckBoxFor instead of @Html.EditorFor.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39811792
It doesn't matter whether I use EditorFor or CheckBoxFor because either way I need a template.

I don't know why the bool type isn't triggering the template - do I have it define wrong?
I also don't know why the checkbox isn't being checked.
LVL 16

Accepted Solution

SriVaddadi earned 2000 total points
ID: 39908374
Rename your template to Boolean.ascx. That should do the trick

Author Closing Comment

ID: 39909212
Thank you.  That worked.  If I wanted to create one for integers would I name it Integer or Int?
LVL 16

Expert Comment

ID: 39909366
Not both. You name it Int32

Author Comment

ID: 39910095

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question