UpdateFrom in MVC Preview 2

Last post 11-05-2009 3:48 AM by levib. 9 replies.

Sort Posts:

  • UpdateFrom in MVC Preview 2

    03-10-2008, 8:24 PM
    • Member
      6 point Member
    • AlexBecker
    • Member since 01-29-2008, 1:29 PM
    • Buenos Aires, Argentina
    • Posts 19

    What happened to UpdateFrom in Preview 2?. How come it's not an extension method anymore? :S

    Thx 

  • Re: UpdateFrom in MVC Preview 2

    03-10-2008, 10:55 PM
    Answer
    • Participant
      846 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 192
    • AspNetTeam

     One of the things we wanted to do was move away from hanging any extensions off of Object (which is where UpdateFrom() used to live). It's now located in System.Web.Mvc.BindingHelperExtensions (which I think will get renamed) as a static method).

  • Re: UpdateFrom in MVC Preview 2

    03-11-2008, 9:39 AM
    • Member
      93 point Member
    • katokay
    • Member since 12-03-2006, 4:37 PM
    • Posts 50

     Seems like it might be nice as Request.Form.DeserializeTo, Which would also allow for Request.QueryString.DeserializeTo, etc.

  • Re: UpdateFrom in MVC Preview 2

    03-11-2008, 6:08 PM
    • Participant
      846 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 192
    • AspNetTeam

    I like it - very nice - perhaps Request.Serialize() and Request.Deserialize(). This is why Phil and Eilon love me... I feel some proto-code coming...

  • Re: UpdateFrom in MVC Preview 2

    03-12-2008, 5:17 AM
    • Member
      6 point Member
    • kombatsanta
    • Member since 03-07-2008, 3:18 PM
    • Posts 8

    So how would be the "correct" way to update an object? I mean instead of doing the following:

      

    public void Create()

    {

    Product product = new Product();

    product.UpdateFrom(Request.Form);

    northwind.AddProduct(product);

    northwind.SubmitChanges();

    RedirectToAction(
    "List");

    }

    You would do it like? 

     

  • Re: UpdateFrom in MVC Preview 2

    03-12-2008, 7:02 AM
    • Member
      2 point Member
    • nicosabena
    • Member since 03-12-2008, 10:58 AM
    • Posts 6

    It seems that you just need to change the line:

    product.UpdateFrom(Request.Form);

    to:

    BindingHelperExtensions.UpdateFrom(product, Request.Form);


     

  • Re: UpdateFrom in MVC Preview 2

    03-12-2008, 8:50 AM
    • Member
      6 point Member
    • kombatsanta
    • Member since 03-07-2008, 3:18 PM
    • Posts 8

    Hmmm... yeah. That worked for my Create method:

    public void Create()

    {

    Product product = new Product();BindingHelperExtensions.UpdateFrom(product, Request.Form);

    northwind.AddProduct(product);

    northwind.SubmitChanges();

    RedirectToAction(
    "List");

    }

    But in my Add method I still have to do this:

    public void Update(int id, string ProductName, int CategoryID, int SupplierID, Decimal UnitPrice)

    {

    Product product = northwind.GetProductById(id);product.ProductName = ProductName;

    product.CategoryID = CategoryID;

    product.SupplierID = SupplierID;

    product.UnitPrice = UnitPrice;

    northwind.SubmitChanges();RedirectToAction("List");

     

    }

    instead of this:

    public void Update(int id)

    {

    Product product = northwind.GetProductById(id);BindingHelperExtensions.UpdateFrom(product, Request.Form);

    northwind.SubmitChanges();

    RedirectToAction(
    "List");

     

    }

  • Re: UpdateFrom in MVC Preview 2

    03-13-2008, 6:04 PM
    • Member
      93 point Member
    • katokay
    • Member since 12-03-2006, 4:37 PM
    • Posts 50

     http://www.hanselman.com/blog/ImJustACavemanTheHanselmanCorollaryToTheClarkeWheelerLaws.aspx

    Yes, I agree it has a nice ring to it. Big Smile 

  • Re: UpdateFrom in MVC Preview 2

    11-04-2009, 10:03 PM
    • Member
      2 point Member
    • quentins
    • Member since 10-14-2009, 8:01 PM
    • Posts 12

    I was hoping MVC 2 would have fixed the problem with TimeStamps.  Using either:

    BindingHelperExtensions.UpdateFrom(e_UserAccount, Request.Form);

    or

    e_UserAccount.UpdateFrom(Request.Form);

    They both blow up with an exception:

       No type converter available for type: System.Byte[]


    So a secondary question would be how do you add additional type converters especially one for byte[]

  • Re: UpdateFrom in MVC Preview 2

    11-05-2009, 3:48 AM
    • Contributor
      4,545 point Contributor
    • levib
    • Member since 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 785

    MVC 2 Preview 2 model binders have built-in support for properties of type byte[] and System.Linq.Binary.  I recommend using one of those instead of the UpdateFrom() extension method.

    BTW - This thread is a year and half old.  Please open a new thread if you have additional questions, or else they're likely to be overlooked by the community.

Page 1 of 1 (10 items)