Solved

bool editor template in mvc4

Posted on 2014-01-24
9
526 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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 …
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…

776 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