Does Object.UpdateFrom Cast?

Last post 12-27-2007 10:29 PM by pbromberg. 5 replies.

Sort Posts:

  • Does Object.UpdateFrom Cast?

    12-26-2007, 11:58 PM
    • Participant
      983 point Participant
    • ChadThiele
    • Member since 04-27-2003, 2:43 AM
    • Japan
    • Posts 274

    My website, spelldamage.com, has a rather large form that users can enter data, check boxes, and select option in. To assist the controller in passing the data to the model, I'm writing a wrapper class that has properties named the same as the fields in the form. I'm doing this so I can just call object.UpdateFrom(request.Form). Some of the fields are text, some checkboxes, some selectors, etc... The properties of the class are types that the model would want to receive, such as double, bool, etc...

    My question is, does the object.UpdateFrom helper read the type the property is expecting and cast the value to the correct type? For example, can it cast a checkbox to a boolean or a text field to a double?

    Thanks in advance! 

    Did I answer your question(s)? Phweew...
    Filed under: ,
  • Re: Does Object.UpdateFrom Cast?

    12-27-2007, 7:59 AM
    Answer

    ChadThiele:
    can it cast a checkbox to a boolean or a text field to a double?
     

    It sure can. As long as the type of the property (or the property itself) has a TypeConverter associated with it, the UpdateFrom method will use that type converter to convert from string to the property type. The basic .net types and whole bunch of other types that come in the class library all have type converters already.

    __________________________________________
    Sergio Pereira
    http://devlicio.us/blogs/sergio_pereira/
  • Re: Does Object.UpdateFrom Cast?

    12-27-2007, 9:33 PM
    • Participant
      983 point Participant
    • ChadThiele
    • Member since 04-27-2003, 2:43 AM
    • Japan
    • Posts 274

    Thanks! I think I've read somewhere that checkboxes don't map to boolean value automatically with .UpdateFrom, is that true? I haven't gotten to a point in my application that I can test it with yet...

    Also, how do you handle this scenario? In the view, I've got some <select> fields that are hard coded. I pass the view some information that should tell the form which <option> should be selected when the view is shown. In web forms asp.net, I'd just tell the as dropdownlist server control what the SelectedIndex was, it would do the rest. Is there a helper that can handle this, when each <option> is hard coded (not dynamically generated at run-time)?

    Did I answer your question(s)? Phweew...
  • Re: Does Object.UpdateFrom Cast?

    12-27-2007, 9:44 PM
    Answer

     For the select fields you can use the Html.Select( .... ) extension method. One of the parameters is the collection of options (dictionary or else). If you need a fixed list, you can always pass in a fixed collection of options.

    For the checkboxes, I honestly haven't tried them yet, but I imagine that if the value of the checkbox is "true" then the TypeConverter should convert easily to a boolean property. 

    __________________________________________
    Sergio Pereira
    http://devlicio.us/blogs/sergio_pereira/
  • Re: Does Object.UpdateFrom Cast?

    12-27-2007, 9:52 PM
    • Participant
      983 point Participant
    • ChadThiele
    • Member since 04-27-2003, 2:43 AM
    • Japan
    • Posts 274

    sergiopereira:

     For the select fields you can use the Html.Select( .... ) extension method. One of the parameters is the collection of options (dictionary or else). If you need a fixed list, you can always pass in a fixed collection of options.

    For the checkboxes, I honestly haven't tried them yet, but I imagine that if the value of the checkbox is "true" then the TypeConverter should convert easily to a boolean property. 

     

    That's true, I guess sometimes web forms makes it almost too easy to do something... makes you forget how it used to be done. :D 

    Did I answer your question(s)? Phweew...
  • Re: Does Object.UpdateFrom Cast?

    12-27-2007, 10:29 PM
    • Contributor
      3,152 point Contributor
    • pbromberg
    • Member since 06-25-2002, 5:13 AM
    • Orlando FL
    • Posts 500
    • TrustedFriends-MVPs

    According to Rob Conery's blog:

    If there is a type-conversion error (or other error), we wait until the end of the method, and then throw a PopulateTypeException. This exception gathers all the exceptions into a collection for you to evaluate in your code. The important thing here is that if there is an error - the binding doesn’t stop, it continues to “do it’s best” and then lets you know there was a problem.

Page 1 of 1 (6 items)