First Last

First Last

  • 1k
  • 648
  • 67.2k

asp.net mvc, razor partial view - boolean property not showing

Dec 2 2020 3:48 PM
Here is a JQuery Datatable where I select the mode - edit, details or delete.
 
The 1 row shows that the boolean PublishedSwitch = true.
 
 
For the edit and delete modes, the boolean - PublishedSwitch (as a radio button) will NOT show. However,
it is passed in the corresponding view model with a value = true.
 
Also, for the edit mode, if I click the save button, the view model 'required' data annotation kicks in stating
"Publish Option required". It does not seem to recognize that it came in with a value = true.
 
If I select the radio button, the validation passes.
 
The edit mode partial view is rendered in a Bootstrap modal whereby I send it the properties in a view model.
The delete view-only partial view is rendered in a Bootstrap modal whereby I send it the properties in a view model.
 
(For sake of keeping it light, I will not show the delete mode code).
 
For the details mode, the boolean - PublishedSwitch (as a radio button) shows just fine.
 
At the bottom, I show the code for the view-only details partial view.
 
The view-only details partial view is rendered in a Bootstrap modal whereby I send it the properties in a view model.
 
For the edit mode:
 
Here is a screen shot depicting the view model just before it is sent to the partial view.
 
 
Here's the partial view - _EditGbngUpdate.cshtml:
  1. @model GbngWebClient.Models.GbngUpdateDetailForMaintVM    
  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. <div id="modalView" class="modal-dialog">    
  13.     <div class="modal-content">    
  14.         <div class="modal-header">    
  15.             <button type="button" id="xClose" class="close" aria-hidden="true">×button>    
  16.     
  17.             <h4 class="modal-title"><strong>Edit Gbng Updatestrong>h4>    
  18.         </div>    
  19.     
  20.         @using (Ajax.BeginForm("EditGbngUpdate""GbngUpdateMaint"nullnew AjaxOptions { HttpMethod = "Post", OnSuccess = "UpdateGbngUpdateSuccess" }, new { @class = "form-horizontal", role = "form" }))    
  21.         {    
  22.             <div class="modal-body">    
  23.                 <div class="form-horizontal">    
  24.                     @Html.ValidationSummary(true""new { @class = "text-danger" })    
  25.     
  26.                     @Html.HiddenFor(model => model.GbngUpdateId)    
  27.     
  28.                     <div class="form-group">    
  29.                         @Html.LabelFor(model => model.GbngUpdateTitle, htmlAttributes: new { @class = "control-label col-md-2 manadatory" })    
  30.                         <div class="col-md-10">    
  31.                             @Html.EditorFor(model => model.GbngUpdateTitle, new { htmlAttributes = new { @class = "form-control" } })    
  32.                             @Html.ValidationMessageFor(model => model.GbngUpdateTitle, ""new { @class = "text-danger" })    
  33.                         div>    
  34.                     </div>    
  35.     
  36.                     <div class="form-group">    
  37.                         @Html.LabelFor(model => model.GbngUpdateContent, htmlAttributes: new { @class = "control-label col-md-2 manadatory" })    
  38.                         <div class="col-md-10">    
  39.                                  @Html.TextAreaFor(model => model.GbngUpdateContent, new { htmlAttributes = new { @class = "form-control" } })    
  40.                             @Html.ValidationMessageFor(model => model.GbngUpdateContent, ""new { @class = "text-danger" })    
  41.                         div>    
  42.                     </div>    
  43.     
  44.                     <div class="form-group">    
  45.                         @Html.LabelFor(model => model.PublishedSwitch, htmlAttributes: new { @class = "control-label col-md-2 manadatory" })    
  46.                         <div class="col-md-10">    
  47.                             Publish    
  48.                             @Html.RadioButtonFor(model => model.PublishedSwitch, true, htmlAttributes: new { @id = "PublishedSwitchTrue" })    
  49.                             Un-Publish    
  50.                             @Html.RadioButtonFor(model => model.PublishedSwitch, false, htmlAttributes: new { @id = "PublishedSwitchFalse" })    
  51.                         </div>    
  52.                         @Html.ValidationMessageFor(model => model.PublishedSwitch, ""new { @class = "text-danger" })    
  53.                     </div>    
  54.     
  55.                     @if (Model.PublishedSwitch == false)    
  56.                     {    
  57.                         <div>    
  58.                             <br />    
  59.                             <strong 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>    
  60.                         </div>    
  61.                     }    
  62.                 </div>    
  63.             </div>    
  64.     
  65.             <div class="modal-footer">    
  66.                 <div class="form-group">    
  67.                     <div class="col-md-offset-2 col-md-10">    
  68.                         <input type="submit" class="btn btn-primary" value="Save" />    
  69.                         <button type="button" id="buttonClose" class="btn btn-default">Closebutton>    
  70.                     </div>    
  71.                </div>    
  72.             </div>    
  73.     
  74.             // To stop forgery - CSRF.    
  75.             @Html.AntiForgeryToken()    
  76.         }    
  77.     </div>    
  78. </div>    
  79.     
  80. <script type="text/javascript">    
  81.     $(function () {    
  82.         $('#xClose').on('click'function () {    
  83.             $('#modalView').modal('hide');    
  84.     
  85.             let myurl = "/GbngUpdateMaint/Index";    
  86.     
  87.             window.location.href = myurl;    
  88.         })    
  89.     
  90.         $('#buttonClose').on('click'function () {    
  91.             $('#modalView').modal('hide');    
  92.     
  93.             let myurl = "/GbngUpdateMaint/Index";    
  94.     
  95.             window.location.href = myurl;    
  96.         })    
  97.     })    
  98.     
  99.     tinymce.init({ selector: 'textarea' });    
  100. </script>  
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 { getset; }    
  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 { getset; }    
  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 { getset; }    
  20.     
  21.         [Required(ErrorMessage = "Publish Option required")]    
  22.         [Display(Name = "Publish Option: ")]    
  23.         public bool PublishedSwitch { getset; }    
  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. <div class="modal-dialog">    
  13.     <div class="modal-content">    
  14.         <div class="modal-header">    
  15.             <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×button>    
  16.     
  17.             <h4 class="modal-title"><strong>Gbng Update's Detailsstrong>h4>    
  18.         </div>    
  19.     
  20.         @using (Ajax.BeginForm(null))    
  21.         {    
  22.             <div class="modal-body">    
  23.                 <div class="form-horizontal">    
  24.                      @Html.ValidationSummary(true""new { @class = "text-danger" })    
  25.     
  26.                     <div class="form-group">    
  27.                         @Html.LabelFor(model => model.GbngUpdateTitle, htmlAttributes: new { @class = "col-md-2" })    
  28.                         <div class="col-md-10">    
  29.                             @* Note: I used EditorFor here instead of TextAreaFor as the width was too small. *@    
  30.                             @Html.EditorFor(model => model.GbngUpdateTitle, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  31.                         </div>    
  32.                     </div>    
  33.     
  34.                     <div class="form-group">    
  35.                         @Html.LabelFor(model => model.GbngUpdateContent, htmlAttributes: new { @class = "col-md-2" })    
  36.                         <div class="col-md-10">    
  37.                             @* Multi-line text box. *@    
  38.                             @* Note: I had to set the htmlAttributes this way to get it to be disabled. *@    
  39.                             @*           If I use: new { htmlAttributes = new { @disabled = "disabled" } }, it will NOT be disabled. *@    
  40.                             @Html.TextAreaFor(model => model.GbngUpdateContent, htmlAttributes: new { @disabled = "disabled" })    
  41.                         </div>    
  42.                     </div>    
  43.     
  44.                     <div class="form-group">    
  45.                         @Html.LabelFor(model => model.PublishedSwitch, htmlAttributes: new { @class = "col-md-2" })    
  46.                         <div class="col-md-5">    
  47.                             Publish    
  48.                             @Html.RadioButtonFor(model => model.PublishedSwitch, true, htmlAttributes: new { @disabled = "disabled" })    
  49.                             Un-Publish    
  50.                             @Html.RadioButtonFor(model => model.PublishedSwitch, false, htmlAttributes: new { @disabled = "disabled" })    
  51.                         </div>    
  52.                     </div>    
  53.     
  54.                     <div class="form-group">    
  55.                         @Html.LabelFor(model => model.PublishedDateTime, htmlAttributes: new { @class = "col-md-2" })    
  56.                         <div class="col-md-5">    
  57.                             @Html.EditorFor(model => model.PublishedDateTime, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  58.                         </div>    
  59.                     </div>    
  60.     
  61.                     <div class="form-group">    
  62.                         @Html.LabelFor(model => model.AlertSentSwitch, htmlAttributes: new { @class = "col-md-2" })    
  63.                         <div class="col-md-5">    
  64.                             @Html.EditorFor(model => model.AlertSentSwitch, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  65.                         </div>    
  66.                     </div>    
  67.     
  68.                     <div class="form-group">    
  69.                         @Html.LabelFor(model => model.CreatedDateTime, htmlAttributes: new { @class = "col-md-2" })    
  70.                         <div class="col-md-5">    
  71.                             @Html.EditorFor(model => model.CreatedDateTime, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  72.                         </div>    
  73.                     </div>    
  74.     
  75.                     <div class="form-group">    
  76.                         @Html.LabelFor(model => model.CreatorsUserName, htmlAttributes: new { @class = "col-md-2" })    
  77.                         <div class="col-md-5">    
  78.                             @Html.EditorFor(model => model.CreatorsUserName, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  79.                         </div>    
  80.                     </div>    
  81.     
  82.                     <div class="form-group">    
  83.                         @Html.LabelFor(model => model.ModifiedDateTime, htmlAttributes: new { @class = "col-md-2" })    
  84.                         <div class="col-md-5">    
  85.                             @Html.EditorFor(model => model.ModifiedDateTime, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  86.                         </div>    
  87.                     </div>    
  88.     
  89.                     <div class="form-group">    
  90.                         @Html.LabelFor(model => model.ModifiersUserName, htmlAttributes: new { @class = "col-md-2" })    
  91.                         <div class="col-md-5">    
  92.                             @Html.EditorFor(model => model.ModifiersUserName, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })    
  93.                         </div>    
  94.                     </div>    
  95.                 </div>    
  96.             </div>    
  97.     
  98.             <div class="modal-footer">    
  99.                 <div class="form-group">    
  100.                     <div class="col-md-offset-2 col-md-10">      
  101.                         <button type="button" class="btn btn-default" data-dismiss="modal">Closebutton>    
  102.                     </div>    
  103.                 </div>    
  104.             </div>    
  105.         }    
  106.     </div>    
  107. </div>  
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 { getset; }    
  9.     
  10.         [Display(Name = "Title: ")]    
  11.         public string GbngUpdateTitle { getset; }    
  12.     
  13.         [Display(Name = "Content: ")]    
  14.         public string GbngUpdateContent { getset; }    
  15.     
  16.         [Display(Name = "Publish Option: ")]    
  17.         public bool PublishedSwitch { getset; }    
  18.     
  19.         [Display(Name = "Published Date: ")]    
  20.         public DateTime? PublishedDateTime { getset; }    
  21.     
  22.         [Display(Name = "Email Alert Sent: ")]    
  23.         public bool AlertSentSwitch { getset; }    
  24.     
  25.         [Display(Name = "Modified Date: ")]    
  26.         public DateTime ModifiedDateTime { getset; }    
  27.     
  28.         [Display(Name = "Modifier:")]    
  29.         public string ModifiersUserName { getset; }    
  30.     
  31.          [Display(Name = "Created Date: ")]    
  32.         public DateTime CreatedDateTime { getset; }    
  33.     
  34.         [Display(Name = "Creator: ")]    
  35.         public string CreatorsUserName { getset; }    
  36.     }    
  37. }  
Here is a screen shot depicting the view where the PublishSwith (as a radio button) is shown just fine.
 
 

Answers (7)