javascript - MVC 4 display/hide a textbox based on a checkbox check -


new mvc--was never of front-end web developer (mostly backend), i'm not privy design on front-end. now, i'm trying use mvc without original webform knowledge...that being said, please give me pointers/links/good ideas on how handle following:

i have textbox field want show/hide based on checkbox. have these fields in view @using (html.beginform()...

should change attribute on text box in javascript or controller action? if use javascript, i'm having hard time getting values in beginform, if use controller action, i'm not sure how/what pass to/from in controller action.

i using jquery if want manipulate dom.

here example -

enter image description here

view

@using (html.beginform()) {     @html.checkboxfor(model => model.mybooleanvalue)     <br />     @html.textboxfor(model => model.mytextvalue)     <br />     <input type="submit" value="submit" /> }  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script>     $(function () {         $("#mybooleanvalue").click(function () {             if ($(this).is(':checked')) {                 $("#mytextvalue").show();             }             else {                 $("#mytextvalue").hide();             }         });     }); </script> 

controller

public class homecontroller : controller {     public actionresult index()     {         var model = new myviewmodel         {             mybooleanvalue = true         };         return view(model);     }      [httppost]     public actionresult index(myviewmodel model)     {         if (model.mybooleanvalue)         {             string text = model.mytextvalue;         }          return view(model);     } } 

model

public class myviewmodel {     public bool mybooleanvalue { get; set; }     public string mytextvalue { get; set; } } 

fyi: suggest watch few videos training (free course @ asp.net mvc 5 fundamentals) or read book or 2 before starting it. otherwise, frustrated.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -