Getting Posted ASP.NET MVC Form Data
page 3 of 8
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 45405/ 69

Setting up the Display View

Now that we have the form created we can create a page where we can display the posted results. I am again going to try to keep this as simple as possible. How to render data on a view is within the scope of another article and not this one.

We obviously need to go and create a new view, which is just an "aspx" file in the correct folder. We will later be passing data to this view so that it can be rendered as content for the user to see. This is a fairly easy process, so I will try to make things quick.

Create the View Page

First, just right click on the Home folder since we specified earlier we were going to make this action part of the Home controller. Then you will select the "Add" submenu and from within that submenu you should see "View." Click on that option.

Figure 4: Add new view context menu

From here you should get a small window to fill in the details about the view you are going to create. This is just a little helper to make the view creation a little bit faster and easier. You obviously could have just created the file by hand, but some of the common tasks are listed here and we will be taking advantage of the master page section of the Add View window.

Figure 5: Add View window

On this window we made sure to name the View something that would make sense considering the action we used earlier in our HTML form. We also selected a master page. I might have used a strongly-typed view, but in this demonstration we have not defined any classes yet. Without any classes we do not have a type to set, so there is no reason to use that feature.

Once you click the Add button, the file will be created and opened for you. Now we can start adding in our rendered content. For this simple little demo we will just write out our values that we receive on the page, so we will need to write out our "First Name," "Middle Name," and "Last Name." Before we can actually see this, we will need to create a controller. These are not the same as the pages on web forms. We need to have a controller action show us this view before we can see it.

Listing 2: Display View Markup

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 
    <h2>DisplayUserData</h2>
    
    First Name: <%= ViewData["FirstName"] %> <br />
    Middle Name: <%= ViewData["MiddleName"] %> <br />
    Last Name: <%= ViewData["LastName"] %>
<span style='background:yellow'> </span>
</asp:Content>

View Entire Article

User Comments

Title: Nice explanation   
Name: Michael Rogers
Date: 2009-11-29 9:57:42 PM
Comment:
Thank you for providing such a clear and succinct explanation.
Title: Very informative and well compiled.   
Name: B.P.Mishra
Date: 2009-08-03 5:08:48 AM
Comment:
Dear Brendan
This article gave me new idea about the MVC framework as I am in the process of learning it.Thank you.keepup the goodwork.
Regards
B.P.Mishra
Title: Nice Blog!   
Name: Software Testing India
Date: 2009-07-28 8:15:44 AM
Comment:
Thanks for this essential blog.

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 6:34:08 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search