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

Form Input and Post Scenario

To help illustrate some of the basics of how to handle form input and posting scenarios with the ASP.NET MVC Framework, we are going to build a simple product listing, product creation, and product editing scenario.  It will have three core end-user experiences:

Product Listing By Category

Users will be able to see a listing of all products within a particular product category by navigating to the /Products/Category/[CategoryID] URL:

Figure 1

Add New Product

Users will be able to add a new product to the store by clicking the "Add New Product" link above.  This takes them to the /Products/New URL - where they will be prompted to enter details about a new product to add:

Figure 2

When they hit save, the product will be added to the database, and they will be redirected back to the product listing page.

Edit Product

On the product listing page users can click the "Edit" link next to each product.  This takes them to the /Products/Edit/[ProductID] URL - where they can change the product details and hit the Save button in order to update them in the database:

Figure 3

Our Data Model

We are going to use the SQL Server Northwind Sample Database to store our data.  We'll then use the LINQ to SQL object relational mapper (ORM) built-into .NET 3.5 to model the Product, Category, and Supplier objects that represent rows in our database tables. 

We'll begin by right-clicking on our /Models sub-folder in our ASP.NET MVC project, and select "Add New Item" -> "LINQ to SQL Classes" to bring up the LINQ to SQL ORM designer and model our data objects:

Figure 4

We'll then create a partial NorthwindDataContext class in our project and add some helper methods to it.  We are defining these helper methods for two reasons: 1) it avoids us embedding our LINQ queries directly in our Controller classes, and 2) it will enable us to more easily adapt our Controller to use dependency injection in the future.

The NorthwindDataContext helper methods we'll add look like below:

Figure 5

To learn more about LINQ and LINQ to SQL, please check out my LINQ to SQL series here.


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-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 10:22:15 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search