locked
Checkbox in MVC3 RRS feed

  • Question

  • User308527929 posted

    Hello to all!

    this might be a silly question to all but i'm stuck with a problem....

    I created the checkbox to select the interest in the application.....

    firstly i used the Checkboxlist but gives an error as it could not find the defination

    so i swichtd to CheckboxFor.....now its working fine

    I have only 2 Questions:

    1.By using the Checkboxlist, do i have to explicitly write any definaation file?? if yes pls give the example

    2.using CheckboxFor, how can i access the selected options text and print them on some other page

     

    please Help me as soon as possible

    Thanx in Advance!!! 

    Tuesday, March 13, 2012 1:44 PM

Answers

  • User-1620313041 posted

    The ChexBoxFor just return a boolean....you cn't use to select all elements of a list. If you need this you have to do this manually by using checkboxex that have all the same name but fifferent value:

    <input type='checkbox' name='MyList' value="1"/>

    <input type='checkbox' name='MyList' value="2"/>

    <input type='checkbox' name='MyList' value="6"/>

    <input type='checkbox' name='MyList' value="8"/>

    Now if the ViewModel of the Action method that receives the post expose a property

    List<int> MyList.

    It will be automatically bound with the list of all integers whose checkboxes were checked...no need to write any code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 13, 2012 2:27 PM

All replies

  • User-1620313041 posted

    The ChexBoxFor just return a boolean....you cn't use to select all elements of a list. If you need this you have to do this manually by using checkboxex that have all the same name but fifferent value:

    <input type='checkbox' name='MyList' value="1"/>

    <input type='checkbox' name='MyList' value="2"/>

    <input type='checkbox' name='MyList' value="6"/>

    <input type='checkbox' name='MyList' value="8"/>

    Now if the ViewModel of the Action method that receives the post expose a property

    List<int> MyList.

    It will be automatically bound with the list of all integers whose checkboxes were checked...no need to write any code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 13, 2012 2:27 PM
  • User-1005219520 posted

    firstly i used the Checkboxlist but gives an error as it could not find the defination

    There is no Checkboxlist  in ASP.NET MVC

    Go through my intro tutorials - that will get you started so you're better equiped to search for answers.

    MVC  3  Intro to ASP.NET MVC
    MVC  4 Beta   Intro to ASP.NET MVC

    Tuesday, March 13, 2012 2:30 PM