Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3797 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET MVC > How to create MVC outbound URL’s (MVC Tutorial number 9)?

How to create MVC outbound URL’s (MVC Tutorial number 9)?

Article posted by Questpond on 9/14/2011 | Views: 14455 | Category: ASP.NET MVC | Level: Advance | Points: 250 red flag


This is the 9th MVC (Model view controller) tutorial and in this article we will try to create outbound navigation links using MVC framework. In case you are completely new to MVC (Model view controller), do have a look at the previous articles and videos given in the table below for quick start.

How to create MVC outbound URL’s (MVC Tutorial number 9)?

 

Introduction

This is the 9th MVC (Model view controller) tutorial and in this article we will try to create outbound navigation links using MVC framework. In case you are completely new to MVC (Model view controller), do have a look at the previous articles and videos given in the table below for quick start.

Article description

MVC ( Model view controller) videos

MVC ( Model view controller) Article links

A simple Hello world ASP.NET MVC ( Model view controller) application.

Hello word MVC Tutorial video

http://www.dotnetfunda.com/articles/article1297-how-to-create-a-simple-hello-world-aspnet-mvc-tutorial-no-1-.aspx 

In this video we will see how we can share data between controller and the view using view data.

MVC Tutorial video 2 Viewdata

http://www.dotnetfunda.com/articles/article1310-how-to-pass-data-from-controllers-to-views-in-aspnet-mvc-tutorial-no-2-.aspx 

In this article we will create a simple customer model, flourish the same with some data and display the same in a view.

MVC Tutorial video 3 Simple model and view example

http://www.dotnetfunda.com/articles/article1317-how-to-create-a-simple-model-in-aspnet-mvc-and-display-the-same-tutorial-.aspx

In this article we will create a simple customer data entry screen with some validation on the view.

MVC tutorial video 4 Simple customer data entry screen.

http://www.dotnetfunda.com/articles/article1388-how-to-create-a-simple-aspnet-mvc-data-entry-screen-tutorial-no-4-.aspx 

This article will demonstrate how to expedite your MVC development process using HTML helper classes.

NA

http://www.dotnetfunda.com/articles/article1415-how-to-create-mvc-model-view-controller-views-faster-by-using-html-helper-.aspx 

This article demonstrates how we can do unit testing in MVC (Model view controller) applications.

NA

http://www.dotnetfunda.com/articles/article1429-how-can-we-do-unit-test-in-mvc-model-view-controller-application-tutoria-.aspx

This article demonstrates how we can define custom routing in MVC applications. NA http://www.dotnetfunda.com/articles/article1485-what-is-mvc-model-view-controller-routing-tutorial-number-7-.aspx
This article demonstrates how we can validate data passed in MVC URL’s. NA http://www.dotnetfunda.com/articles/article1527-how-to-validate-data-provided-in-mvc-urls-mvc-tutorial-number-8-.aspx


My half life has been spent on writing interview questions for Microsoft technology and I hope I keep writing them. You can see my video tutorial for the same by clicking on .NET,ASP.NET,C#,MVC Interview questions and answers

When we talk about web applications end users would like to navigate from one page to other page. So as a simple developer your first thought would be to just give page names as shown in the below figure.

So for example if you want to go and browse from home.aspx to about.aspx give the anchor hyper link page name and things should be fine.

By doing that you are violating MVC principles. MVC principle says that hit should first come to the controller but by specifying <a href=”Home.aspx”> the first hit comes to the view. This bypasses your controller logic completely and your MVC architecture falls flat.
 


Ideally the actions should direct which page should be invoked. So the hyperlink should have actions in the anchor tags and not the page names i.e. direct view name.

Step 1:- Create views

Lets create three views as shown in the below figure “Home”,”About” and “Product”.


Let’s create a simple navigation between these 3 pages as shown below. From the home view we would like to navigate to about and product view. From about and product view we would like to navigate back to the home view.


Step 2 :- Create controller for the views

Next step is to define controller actions which will invoke these views. In the below code snippet we have defined 3 actions “GotoHome” (this invokes home view), “Aboutus” ( this invokes the about view) and “SeeProduct” ( this invokes product view).

public class SiteController : Controller
{
//
// GET: /Site/

public ActionResult GotoHome()
{
return View("Home");
}

public ActionResult AboutUs()
{
return View("About");
}

public ActionResult SeeProduct()
{
return View("Product");
}
}

Step 3:- Provide actions in the link

To invoke the actions rather than the views we need to specify the actions in the anchor tag as shown in the below code snippet.
This is products

<a href="GotoHome">Go Home</a><br />
<a href="Aboutus">About us</a><br />

If you want to create the anchor links using the HTML helper classes you can use the action link function as shown in the below code snippet.

<%= Html.ActionLink("Home","Gotohome") %>

The above code was for the products page , you can do the same type of navigations for the about us and the home page.

This is About us
<a href="GotoHome">Go Home</a><br />
<a href="SeeProduct">See Product</a><br />
This is home page
<br />
<a href="SeeProduct">See Product</a><br />
<a href="Aboutus">About us</a><br />

</div>

Step 4:- Enjoy your navigation

Once you have specified the actions inside the link you navigate between home, about and products page.


While navigating you can see how the URL’s are pointing to the actions rather than absolute page names like home.aspx , aboutus.aspx etc which violates the complete MVC principle.

 

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:0 year(s)
Home page:http://www.questpond.com
Member since:Wednesday, September 03, 2008
Level:Starter
Status: [PanelMember] [Member] [Microsoft_MVP] [MVP] [Administrator]
Biography:

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers

>> Write Response - Respond to this post and get points
Related Posts

In today's dot.net world MVC architecture was the new hot cake released in 2009 . In 2012 MVC4 has been released and the developers are moving towards Asp.Net MVC. Let us see why MVC than Web-forms and what are the advantages of MVC over Webforms .

When ever we create a controller .By default we will get an action result method .We have different types action results .Let us have an overview of available action results and where to use appropriate action result

In this article we are going to know the software requirements to work with asp.net mvc4 and Supported Templates , Folder structure of an Asp.Net MVC4 application

* Introduction To MVC * Why Asp.net MVC over Asp.net Web Forms * MVC Architecture-Overview * MVC Request Life Cycle * Basic Example –Explain

This is the 8th MVC (Model view controller) tutorial and in this article we try to understand how we can validate data passed in MVC URL’s. In case you are completely new to MVC (Model view controller), do have a look at the previous articles and videos given in the table below for quick start.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 11:14:22 PM