0

I created an editor template for Boolean types, my problem it's when model attribute it's not nullable, the default behaviour of the checkbox are modified, for example if themodel value is true then the checkbox in the view appears without checked. What am I doing wrong??? this is the code.

@model Nullable<bool>

@{
    var listItems = new[]
    {   
        new SelectListItem { Value = "null", Text = "Sin Valor" },
        new SelectListItem { Value = "true", Text = "Si" },
        new SelectListItem { Value = "false", Text = "No" }
    };  

}

@if (ViewData.ModelMetadata.IsNullableValueType)
{

    @Html.DropDownListFor(model => model.Value, listItems)
}
else
{
    @Html.CheckBox("", ViewData.TemplateInfo.FormattedModelValue)
}

2 Answers 2

1

Not sure what ViewData.ModelMetadata.v that you are using in your checkbox means. Have you tried:

@model Nullable<bool>

@{
    var listItems = new[]
    {   
        new SelectListItem { Value = "null", Text = "Sin Valor" },
        new SelectListItem { Value = "true", Text = "Si" },
        new SelectListItem { Value = "false", Text = "No" }
    };  
}

@if (ViewData.ModelMetadata.IsNullableValueType)
{
    @Html.DropDownList("", new SelectList(listItems, "Value", "Text", Model))
}
else
{
    @Html.CheckBox("", Model.Value)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I modified the question for you could see the my old code. And what's exactly i'm trying to do
0

This example shows in detail the implementation of boolean templates for a dropdownlist that contains Yes, No and Not Set values and also handles null bool values. Inspired from Darin Dimitrov and Jorge above - Thank you.

Model Student.cs

    [Display(Name = "Present:")]
    [UIHint("YesNo")]
    public bool? IsPresent { get; set; }

DisplayTemplates: YesNo.cshtml

@model Nullable<bool>

@if (Model.HasValue)
{
    if (Model.Value)
        { <text>Yes</text> }
    else
        { <text>No</text> }
}
else
    { <text>Not Set</text> }

EditorTemplates: YesNo.cshtml

@model Nullable<bool>

@{
    var listItems = new[]
    {   
        new SelectListItem { Value = "null", Text = "Not Set" },
        new SelectListItem { Value = "true", Text = "Yes" },
        new SelectListItem { Value = "false", Text = "No" }
    };  
}

@if (ViewData.ModelMetadata.IsNullableValueType)
{
    @Html.DropDownList("", new SelectList(listItems, "Value", "Text", Model))
}
else
{
    @Html.CheckBox("", Model.Value)
}

View:

    <div class="editor-label">
        @Html.LabelFor(model => model.IsPresent )
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.IsPresent )
        @Html.ValidationMessageFor(model => model.IsPresent )
    </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.