Solved

bool editor template in mvc4

Posted on 2014-01-24
9
533 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
[X]
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
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
Technology Partners: 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

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
 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

696 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