Skip to content
Loading
asp.net mvc, razor partial view - boolean property not showing    
  •                         @Html.ValidationMessageFor(model => model.PublishedSwitch, "", new { @class = "text-danger" })    
  •                         
  •     
  •                     @if (Model.PublishedSwitch == false)    
  •                     {    
  •                         
        
  •                                 
  •                             class="warningdescr">Warning: Before you publish this gbng update, be aware that you will NOT be able to unpublish it after you publish it. Also, once published, you cannot modify it or delete it.strong>    
  •                             
  •                     }    
  •                     
  •                 
  •     
  •             class="modal-footer">    
  •                 class="form-group">    
  •                     class="col-md-offset-2 col-md-10">    
  •                         "submit" class="btn btn-primary" value="Save" />    
  •                         "button" id="buttonClose" class="btn btn-default">Closebutton>    
  •                         
  •                    
  •                 
  •     
  •             // To stop forgery - CSRF.    
  •             @Html.AntiForgeryToken()    
  •         }    
  •         
  •     
  •     
  • "text/javascript">    
  •     $(function () {    
  •         $('#xClose').on('click', function () {    
  •             $('#modalView').modal('hide');    
  •     
  •             let myurl = "/GbngUpdateMaint/Index";    
  •     
  •             window.location.href = myurl;    
  •         })    
  •     
  •         $('#buttonClose').on('click', function () {    
  •             $('#modalView').modal('hide');    
  •     
  •             let myurl = "/GbngUpdateMaint/Index";    
  •     
  •             window.location.href = myurl;    
  •         })    
  •     })    
  •     
  •     tinymce.init({ selector: 'textarea' });    
  •   
  • Here's it's view model - GbngUpdateDetailForMaintVM:
    1. using System.ComponentModel.DataAnnotations;    
    2. using System.Web.Mvc;    
    3.     
    4. namespace GbngWebClient.Models    
    5. {    
    6.     public class GbngUpdateDetailForMaintVM    
    7.     {    
    8.         public int GbngUpdateId { get; set; }    
    9.     
    10.         [Required(ErrorMessage = "Gbng Update Title required")]    
    11.         [MinLength(5, ErrorMessage = "Gbng Update Title Must Be At Least 5 characters")]    
    12.         [Display(Name = "Title: ")]    
    13.         public string GbngUpdateTitle { get; set; }    
    14.     
    15.         [Required(ErrorMessage = "Gbng Update Content required")]    
    16.         [MinLength(10, ErrorMessage = "Gbng Update Content Must Be At Least 10 characters")]    
    17.         [AllowHtml]    
    18.         [Display(Name = "Content: ")]    
    19.         public string GbngUpdateContent { get; set; }    
    20.     
    21.         [Required(ErrorMessage = "Publish Option required")]    
    22.         [Display(Name = "Publish Option: ")]    
    23.         public bool PublishedSwitch { get; set; }    
    24.     }    
    25. }    
    Here is a screen shot depicting the view where the PublishSwith (as a radio button) will NOT show.
     
      
    For the details view-only mode:
     
    In this view-only details view the boolean - PublishedSwitch (as a radio button) - shows just fine.
     
    It's a view-only details partial view presented in a Bootstrap modal whereby I send it the properties in a view model.
     
    Here is a screen shot depicting the view model just before it is sent to the partial view.
     
     
    Here's the partial view - _DetailsGbngUpdate.cshtml:
    1. @model GbngWebClient.Models.GbngUpdateDetailVM    
    2.     
    3. @{    
    4.     Layout = null;    
    5. }    
    6.     
    7. @Scripts.Render("~/bundles/jqueryval")    
    8. @Scripts.Render("~/bundles/jquery")    
    9. @Scripts.Render("~/bundles/bootstrap")    
    10. @Styles.Render("~/Content/css")    
    11.     
    12. class="modal-dialog">    
    13.     class="modal-content">    
    14.         class="modal-header">    
    15.             "button" class="close" data-dismiss="modal" aria-hidden="true">×button>    
    16.     
    17.             class="modal-title">Gbng Update's Detailsstrong>h4>    
    18.         
        
  •     
  •         @using (Ajax.BeginForm(null))    
  •         {    
  •             class="modal-body">    
  •                 class="form-horizontal">    
  •                      @Html.ValidationSummary(true, "", new { @class = "text-danger" })    
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.GbngUpdateTitle, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-10">    
  •                             @* Note: I used EditorFor here instead of TextAreaFor as the width was too small. *@    
  •                             @Html.EditorFor(model => model.GbngUpdateTitle, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.GbngUpdateContent, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-10">    
  •                             @* Multi-line text box. *@    
  •                             @* Note: I had to set the htmlAttributes this way to get it to be disabled. *@    
  •                             @*           If I use: new { htmlAttributes = new { @disabled = "disabled" } }, it will NOT be disabled. *@    
  •                             @Html.TextAreaFor(model => model.GbngUpdateContent, htmlAttributes: new { @disabled = "disabled" })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.PublishedSwitch, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-5">    
  •                             Publish    
  •                             @Html.RadioButtonFor(model => model.PublishedSwitch, true, htmlAttributes: new { @disabled = "disabled" })    
  •                             Un-Publish    
  •                             @Html.RadioButtonFor(model => model.PublishedSwitch, false, htmlAttributes: new { @disabled = "disabled" })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.PublishedDateTime, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-5">    
  •                             @Html.EditorFor(model => model.PublishedDateTime, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.AlertSentSwitch, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-5">    
  •                             @Html.EditorFor(model => model.AlertSentSwitch, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.CreatedDateTime, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-5">    
  •                             @Html.EditorFor(model => model.CreatedDateTime, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.CreatorsUserName, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-5">    
  •                             @Html.EditorFor(model => model.CreatorsUserName, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.ModifiedDateTime, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-5">    
  •                             @Html.EditorFor(model => model.ModifiedDateTime, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  •                             
  •                         
  •     
  •                     class="form-group">    
  •                         @Html.LabelFor(model => model.ModifiersUserName, htmlAttributes: new { @class = "col-md-2" })    
  •                         class="col-md-5">    
  •                             @Html.EditorFor(model => model.ModifiersUserName, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  •                             
  •                         
  •                     
  •                 
  •     
  •             class="modal-footer">    
  •                 class="form-group">    
  •                     class="col-md-offset-2 col-md-10">      
  •                         "button" class="btn btn-default" data-dismiss="modal">Closebutton>    
  •                         
  •                     
  •                 
  •         }    
  •         
  •   
  • Here's the view model - GbngUpdateDetailVM:
    1. using System;    
    2. using System.ComponentModel.DataAnnotations;    
    3.     
    4. namespace GbngWebClient.Models    
    5. {    
    6.     public class GbngUpdateDetailVM    
    7.     {       
    8.         public int GbngUpdateId { get; set; }    
    9.     
    10.         [Display(Name = "Title: ")]    
    11.         public string GbngUpdateTitle { get; set; }    
    12.     
    13.         [Display(Name = "Content: ")]    
    14.         public string GbngUpdateContent { get; set; }    
    15.     
    16.         [Display(Name = "Publish Option: ")]    
    17.         public bool PublishedSwitch { get; set; }    
    18.     
    19.         [Display(Name = "Published Date: ")]    
    20.         public DateTime? PublishedDateTime { get; set; }    
    21.     
    22.         [Display(Name = "Email Alert Sent: ")]    
    23.         public bool AlertSentSwitch { get; set; }    
    24.     
    25.         [Display(Name = "Modified Date: ")]    
    26.         public DateTime ModifiedDateTime { get; set; }    
    27.     
    28.         [Display(Name = "Modifier:")]    
    29.         public string ModifiersUserName { get; set; }    
    30.     
    31.          [Display(Name = "Created Date: ")]    
    32.         public DateTime CreatedDateTime { get; set; }    
    33.     
    34.         [Display(Name = "Creator: ")]    
    35.         public string CreatorsUserName { get; set; }    
    36.     }    
    37. }  
    Here is a screen shot depicting the view where the PublishSwith (as a radio button) is shown just fine.
     
     

    7 Replies

    Know the answer? Post it — somebody with the same question will find it here.

    • To all, the problem is the model binding is getting confused because I named my action parameter (in the action method that was sending the partial view) the same as my model property - publishedSwitch. 
      1. public async Task EditGbngUpdate(int gbngUpdateId, bool publishedSwitch, bool alertSentSwitch).  
      I changed the name of the action parameter - publishedSwitch. Now it works fine. Weird I know! 
       
      A tough one to figure out, but after more research, I discovered the solution in a stackoverflow similar situation. So I give credit to AJ in https://stackoverflow.com/questions/10629671/using-html-radiobuttonfor-with-a-boolean-isnt-writing-checked-checked
      +1
    • Also, there is no checked attribute when there should be.
       
       
      +1
    • Sachin, I'm trying your suggestion in spite of the fact the way I have it setup works fine in my display view. So don't understand why it would work there, but not in my edit view. 
       
      I have per your code.  
      1. <div>  
      2.     <input type="radio" id="IsPublished" name="PublishedSwitch" value="true" @(model.PublishedSwitch == true ? checked: "")>  
      3. div>  
       
      I get
       
      "the name 'model' does not exist in the current context".
       
      The model has the red squilly line under it as well as the :.
      +1
    • Sachin, I have a similar (almost identical) blog maintenance function with edit, details and delete partial views. For that one, I have the same radio button setup for the PublishSwitch. It works fine there for the edit, delete and details. When the view models contains a PublishSwitch value of true, the radio button is selected - shows as true in all views. I compared the 2 sets of code and they are the same. So I don't get why it does not show properly in this case.
      +1
    • Kiran, the point here is that the value comes in as 'true' for both the edit and details view respective view models but the radio button does not show as true (a selected button) like the way it does on in the details view.
      +1
    • Sachin Singh
      In Edit Mode
      under Modal popup, use simple radiobutton with ternary operator , it is lot easier.
      1. "radio" id="IsPublished" name="PublishedSwitch"
      2. value="true" @(model.PublishedSwitch==true?checked:"")> 
       Now your radiobutton will be checked or unchecked as per model value.
      +1
    • Kiran Mohanty
      1. <div class="col-md-10">      
      2.  Publish      
      3. @Html.RadioButtonFor(model => model.PublishedSwitch, true, htmlAttributes:   
      4.     new { @id = "PublishedSwitchTrue" })      
      5. Un-Publish      
      6.  @Html.RadioButtonFor(model => model.PublishedSwitch, false, htmlAttributes:   
      7.     new { @id = "PublishedSwitchFalse" })      
      8. div>      
      9. @Html.ValidationMessageFor(model => model.PublishedSwitch, "", new { @class = "text-                        danger" })      
      10. div>   
      Hi,
      As per my understanding, you are using a single property for both radio buttons and the same property is set for validation. So for "un-publish" state radio button value is false. as soon as you click on any option, "PublishedSwitch" is set to true and hence no validation error.
      +1