ASP.NET MVC Framework (Part 4): Handling Form Edit and Post Scenarios
page 9 of 15
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 59424/ 252

Implementing Add New Product (Part 4 - Cleaning Up Create with the UpdateFrom Method)

Our ProductsController's "Create" Action method is responsible for handling the form posting of our "Add Product" scenario.   It currently handles incoming form parameters as arguments to the action method:

Figure 28

This approach works fine - although for forms involving large amounts of values the method signatures on Actions can start to become a little hard to read.  The code above that sets all of the incoming parameter values to the new Product object is also a little long and monotonous.

If you reference the MVCToolkit assembly, you can optionally take advantage of a useful Extension Method implemented within the System.Web.Mvc.BindingHelpers namespace that can help clean this up a little.  It is called "UpdateFrom" and can be used on any .NET object.  It takes a dictionary of values as an argument, and it will then automatically perform a property assignment on itself for any key that matches a public property on the object.

For example, we could re-write our Create action method above to use the UpdateFrom method like so:

Figure 29

Note: if you want to be more explicit for security reasons and only allow certain properties to be updated, you can also optionally pass a string array of the property names to update to the UpdateFrom method:

Figure 30


View Entire Article

User Comments

Title: Stuck / adding Creat & New to my Controller.cs   
Name: Casey
Date: 2009-07-29 4:08:45 PM
Comment:
Hi,
I'm a newbie as if you don't hear that all day long.

I have setup a project that is to allow me to post information to a SQL database, and view the updated information. I can view just fine, however I'm stuck on where in my code to add the Create, and New Methods. If you would like to look at my code I can post it or email it.

Thank you,

Casey,
Title: text search for an table in asp.net mvcframework   
Name: senthil
Date: 2009-02-07 8:42:22 AM
Comment:
i want to make an text search on a column in a table.
for exampe i type a name in the text box and click a button then it should search the name in the column .

hope u help me

thanks in advance






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2020 ASPAlliance.com  |  Page Processed at 2020-07-05 11:42:10 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search