[BUG]Value from checkbox do not handle as parameter

Last post 12-25-2007 2:16 AM by mogadanez. 12 replies.

Sort Posts:

  • [BUG]Value from checkbox do not handle as parameter

    12-23-2007, 4:08 PM
    • Member
      73 point Member
    • mogadanez
    • Member since 09-22-2005, 5:33 AM
    • Posts 48

    have view:

    <form action='product/update'>
       <input type="checkbox" name="include_details">
    </form>

     controller:

    [ControllerAction]
    public void update ( bool include_details )
    {
    
    }
    but it not work( Converting Exception )
    it even not work if in change type in controoler parameter to object. in this case   include_details == null 
     
     

     

    Filed under:
  • Re: [BUG]Value from checkbox do not handle as parameter

    12-23-2007, 5:46 PM
    • Participant
      1,393 point Participant
    • MaineOne
    • Member since 01-20-2006, 1:00 AM
    • Maine
    • Posts 335

    I do not know much C# but If you want the value of the Checkbox all you should need to do is use Request("include_details")

     

    Sub Update()
                 Dim xxx as Boolean = Request("include_details")
        
    End Sub

    In C# ( don't know the Dim Equivalant off the top of my head, but this should give you an Idea)

    Public Void Update()

    {

    xxx = Request["include_details"]

    }

    aspsksolutions.com

  • Re: [BUG]Value from checkbox do not handle as parameter

    12-23-2007, 5:53 PM

     MaineOne is right. If you look at your form's action attribute "product/update" that is the URL that will be invoked. If you want the checkbox state to be passed in the URL I think you'll need some javascript to put it there right before the form submits, possibly using the onsubmit event of the form.

    Otherwise, just use what MaineOne said in the server side and remove that action parameter. 

    __________________________________________
    Sergio Pereira
    http://devlicio.us/blogs/sergio_pereira/
  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 2:01 AM
    • Member
      73 point Member
    • mogadanez
    • Member since 09-22-2005, 5:33 AM
    • Posts 48

    i  use POST, not GET. 

    sorry,  missed:

    <form action='product/update' method="POST">
    ...

     

  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 11:49 AM

    That doesn't matter for the problem if not getting the value as a parameter to the action method. I was assuming you had a POST there anyway. With POST in your form you will be able to verify the checkbox presence using the Request.Form["checkbox_name"]. 

    To get the checkbox state passed as a method parameter I still think you will need to put it in the form's action before posting. 

    __________________________________________
    Sergio Pereira
    http://devlicio.us/blogs/sergio_pereira/
  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 1:59 PM
    • Participant
      846 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 192
    • AspNetTeam

     The conversion error is because you're trying to convert the default checkbox value of "on" or "off" to a boolean. To get this to work, you need to set a value on the checkbox of "1" or "true" and then do some testing to make sure it's been selected:

     

    bool isChecked=Request.Form["include_details"].ToString()=="1";
      
  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 2:30 PM
    • Member
      73 point Member
    • mogadanez
    • Member since 09-22-2005, 5:33 AM
    • Posts 48

    I have strong opinion that Controller MUST NOT  acceess Request.Form anyway this is interface.

    I have controller with simple and testable  signature. I write controller and than view not  the contrary

    public void Step2( string Name, int Age, bool skip_step )

    and want be possible provide fields from html form to this controller.

    in signature above int field succesfull provided to controller ( except cases when not int values entered - this interesting case too, but about it later. )
    and  chackebox i think must be converted to bool (i think it very logical =))  ) 

     so i want have way for it, some kind of converters etc.

    Now I have one more or less appropriate way:

    use signature:
    public void Step2( string Name, int Age, string skip_step )
    and check that skip_step is not null
    {
         if ( skip_step
     != null )
             // checked
         else
           // unchecked

     

     

  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 3:03 PM
    Answer

     http://www.squaredroot.com/post/2007/12/MVC-Checkboxes-Complaint.aspx

     This blog had a pretty good rundown on how the current CTP handles checkboxes.


     

  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 3:43 PM

    mogadanez:
    I have strong opinion that Controller MUST NOT  acceess Request.Form anyway this is interface.
     

    Are you saying that you think using Request.Form is always to be avoided?? I hope not. How would you handle form posts with several textareas and file uploads for example?  

    __________________________________________
    Sergio Pereira
    http://devlicio.us/blogs/sergio_pereira/
  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 4:19 PM
    • Member
      73 point Member
    • mogadanez
    • Member since 09-22-2005, 5:33 AM
    • Posts 48

    Thanks, Troy Goode has the same solution as me -  provide string param and check for null

    [ControllerAction]

    public void Authenticate( string userName, string password, string rememberMe )

    {

        if( rememberMe != null ){ ... }

    }

  • Re: [BUG]Value from checkbox do not handle as parameter

    12-24-2007, 4:31 PM
    • Member
      73 point Member
    • mogadanez
    • Member since 09-22-2005, 5:33 AM
    • Posts 48

    sergiopereira:

    mogadanez:
    I have strong opinion that Controller MUST NOT  acceess Request.Form anyway this is interface.
     

    Are you saying that you think using Request.Form is always to be avoided?? I hope not. How would you handle form posts with several textareas and file uploads for example?  

    I think in Most of cases.  Code that handle TextAreas must be extracted  to transport level. If we do it in ControllerAction we add one Extra  responsibility to this method and increase his complexity.

    if we have many or complexity form fields we can use something like ( took  from http://hammett.castleproject.org/?p=229 ):

    [ControllerAction]
    public void Create([FormBinder] Product product)

    but anyway not inside ControllerAction.

     

  • Re: [BUG]Value from checkbox do not handle as parameter

    12-25-2007, 1:33 AM
    • Participant
      846 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 192
    • AspNetTeam

    mogadanez:

     

    I have controller with simple and testable  signature. I write controller and than view not  the contrary

     

    I think the issue here is whether you use GET or POST. In this case, for testing, you can mock the Request.Form collection as needed. I'd like to keep this thread on-topic, if you want to move this discussion to a new thread that would help.

    Did your question get answered with regards to checkboxes? 

  • Re: [BUG]Value from checkbox do not handle as parameter

    12-25-2007, 2:16 AM
    • Member
      73 point Member
    • mogadanez
    • Member since 09-22-2005, 5:33 AM
    • Posts 48

    robconery:

    I think the issue here is whether you use GET or POST. In this case, for testing, you can mock the Request.Form collection as needed. I'd like to keep this thread on-topic, if you want to move this discussion to a new thread that would help.

    Yes i can, but not want. this way increase complexity both tests and code. Also this create hidden dependecies. I agree that this  can be moved to new thread. I will enjoy in this topic. But not for me. I Just want  to MVC Framework be best framework after final release. Wink

    robconery:

    Did your question get answered with regards to checkboxes? 

    More or less. i got that this is current behivior of CTP version.

Page 1 of 1 (13 items)