c# - Bind Model property to multiple fields -


i have model string property: securitygroupmapping, want able set using 1 of 2 lists of strings. lists shown based on selection given in radio group assigned bool.

i've looked @ post: mvc 3 bind model property 2 fields (e.g. other title), have different scenario, , hoping there easy way disable field if it's not in use...

<div class="editor-label">     @html.label("choose how assign users security group") </div> <div class="editor-field" style="width: 300px; display: inline;">     @html.radiobuttonfor(m => m.usedefaultgroupsecuritygroup, false) map individual security group     @html.radiobuttonfor(m => m.usedefaultgroupsecuritygroup, true) use default security group </div> <br/> <div id="showusersecuritygroup">     <div class="editor-label">         @html.label("map csv field user's security group")     </div>     <div style="width: 300px; display: inline;">         @html.dropdownlistfor(m => m.securitygroupmapping, new selectlist(model.csvfields), "please select")     </div> </div> <div id="showgroupsecuritygroup">     <div class="editor-label">         @html.label("default security group users")     </div>     <div style="width: 300px; display: inline;">         @html.dropdownlistfor(m => m.securitygroupmapping, new selectlist(model.securitygroups), "please select")     </div> </div>  <script> $("input[name=\"usedefaultgroupsecuritygroup\"]:radio").change(     function () {         var selectedvalue = $("input:radio[name=usedefaultgroupsecuritygroup]:checked").val();          if (selectedvalue === "true") {             $("#showgroupsecuritygroup").show();             $("#showusersecuritygroup").hide();         }         else {             $("#showgroupsecuritygroup").hide();             $("#showusersecuritygroup").show();         }     } ); </script> 

currently, if select first radio button, , populate first field, comes through in postback correctly. if select second radio button , populate second field, property comes null. possible disable field within hidden div?

edit: suggestion disable element worked show hide in end - didn't think simple assigning each drop down different id, , disabling in jquery.

@html.dropdownlistfor(m => m.securitygroupmapping, new selectlist(model.securitygroups), "please select", new { id = "groupsecuritygroup" })  $("input[name=\"usedefaultgroupsecuritygroup\"]:radio").change(     function () {         var selectedvalue = $("input:radio[name=usedefaultgroupsecuritygroup]:checked").val();          if (selectedvalue === "true") {             $("#usersecuritygroup").prop("disabled", true);             $("#groupsecuritygroup").prop("disabled", false);             $("#showgroupsecuritygroup").show();             $("#showusersecuritygroup").hide();         }         else {             $("#usersecuritygroup").prop("disabled", false);             $("#groupsecuritygroup").prop("disabled", true);             $("#showgroupsecuritygroup").hide();             $("#showusersecuritygroup").show();         }     } ); 

option 1

based on radio button selection should load dropdown different selectlist, instead of hiding/showing.

option 2 @evan mulawski suggested can use 2 separate fields on view , viewmodel. might easiest , simple it.

option 3

instead of hiding/showing dropdowns can move them outside/inside of form tag.

    $("input[name=\"usedefaultgroupsecuritygroup\"]:radio").change(            function ()             {                var selectedvalue = $("input:radio[name=usedefaultgroupsecuritygroup]:checked").val();                 if (selectedvalue === "true") {                   $("#showusersecuritygroup").detach().insertafter($(this).closest("form"))                   $("#showgroupsecuritygroup").detach().appendto($(this).closest("form"))                                            }                else                 {                   $("#showusersecuritygroup").detach().appendto($(this).closest("form"))                   $("#showgroupsecuritygroup").detach().insertafter($(this).closest("form"))                                                         }            }); 

option 4 - easiest of all

as @stephen muecke suggested , disabling/enabling elements instead of hide/show.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -