Solved

bool editor template in mvc4

Posted on 2014-01-24
9
519 Views
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();   
     if(ViewData.ModelMetadata.AdditionalValues.ContainsKey("HtmlAttributes"))      
         htmlAttributes = (ENL.HtmlPropertiesAttribute) ViewData.ModelMetadata.AdditionalValues["HtmlAttributes"];   
     htmlAttributes.HtmlAttributes().Add("class", "text-box single-line " + htmlAttributes.CssClass); 
 %>
<span>          
    <%= Html.Label(ViewData.ModelMetadata.DisplayName, new { @class = "editor-label" })%> :        
    <%= Html.CheckBox(String.Empty, ViewData.TemplateInfo.FormattedModelValue)%>                                         
</span>

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
{
    [UIHint("PrimaryKey")]
    [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; }
    [DisplayName("Active?")]
//    [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)
        </div>

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!
0
Comment
Question by:dyarosh
  • 4
  • 2
  • 2
9 Comments
 
LVL 96

Expert Comment

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

Did you try this?

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

Author Comment

by:dyarosh
ID: 39808726
I can't use it because it is a template.  I can't access the model directly.
0
 
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)
        </div>

Open in new window


I meant that you could use @Html.CheckBoxFor instead of @Html.EditorFor.
0
 

Author Comment

by:dyarosh
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Accepted Solution

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

Author Closing Comment

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

Expert Comment

by:SriVaddadi
ID: 39909366
Not both. You name it Int32
0
 

Author Comment

by:dyarosh
ID: 39910095
Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now