[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 567
  • Last Modified:

bool editor template in mvc4

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
dyarosh
Asked:
dyarosh
  • 4
  • 2
  • 2
1 Solution
 
Bob LearnedCommented:
I am confused...

Did you try this?

@Html.CheckBoxFor(model => model.IsActive)
0
 
dyaroshAuthor Commented:
I can't use it because it is a template.  I can't access the model directly.
0
 
Bob LearnedCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dyaroshAuthor Commented:
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
 
SriVaddadiCommented:
Rename your template to Boolean.ascx. That should do the trick
0
 
dyaroshAuthor Commented:
Thank you.  That worked.  If I wanted to create one for integers would I name it Integer or Int?
0
 
SriVaddadiCommented:
Not both. You name it Int32
0
 
dyaroshAuthor Commented:
Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now