c# - Radio Buttons tied to a nullable enumeration returning null even if checked -


for project working on, have setup where, in view model, have property nullable three-way enumeration. problem having that, there use case radio buttons disabled, , though radio button being programmatically checked, view model still receiving null value enumeration value, , triggering required validation.

as far rules go, under normal circumstances, reason enum allowed null require user make choice between a, b, or c before being allowed continue, in application working on, there additional pieces of data needed later in process.

question: why that, when user clicks submit button on markup, null value reaching server, triggering validation error? can see in script segment below, in particular case, radio button is being selected.

enumeration

public enum threewayenum {     a,     b,     c } 

view model

public class myviewmodel {     public bool canuseenum     {                 {             // business logic goes here, question not matter.             // question state causes radio buttons not usable on client.             return false;         }     }      [required]     public threewayenum? myenum { get; set; }      // else unimportant question. } 

controller (nothing special, not suspect problem here @ all.)

[httpget] public actionresult myaction(int id) {     myviewmodel model = new myviewmodel(id);     return view(model); }  [httppost] public actionresult myaction(myviewmodel model) {     if(modelstate.isvalid)     {         model.save();         return redirect("someotheraction", "thiscontroller");     }      return view(model); } 

markup

@model myviewmodel  @using(html.beginform("myaction", "thiscontroller") {     @html.hiddenfor(m => m.canuseenum, new { id = "can-use-enums" })      <div class="horz-group">        <div class="span9">select option</div>        <div class="span7">            @html.radiobuttonfor(m => m.myenum, threewayenum.a, new { id = "opt-a" } )            @html.radiobuttonfor(m => m.myenum, threewayenum.b, new { id = "opt-b" } )            @html.radiobuttonfor(m => m.myenum, threewayenum.c, new { id = "opt-c" } )        </div>     </div>      <div class="horz-group">         <button type="submit">continue</button>     </div> } 

script

var $canuseenum = $('#can-use-enums'),     $opta = $('#opt-a'),     $optb = $('#opt-b'),     $optc = $('#opt-c');  $(document).ready(function () {     disableselectionifineligible(); });  function disableselectionifineligible() {     if($canuseenum.val() == 'true')         return;      $opta.attr("disabled", "disabled");     $optb.attr("disabled", "disabled");     $optc.attr("disabled", "disabled");      // selects 'default' option.     $opta.attr("checked", "checked"); } 

as sanity check, commented out lines disable radio buttons, , tried again. sure enough, disabling radio buttons is preventing value being conveyed server.

if disable of radio buttons option, return null value.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -