In this Section we will create a simple hello world program using MVC template.
So we will create a simple controller, attach the controller to simple
index.aspx page and view the display on the browser.
Create a new project by selecting the MVC 2 empty web application template as
shown in the below figure.
Once you click ok, you have a readymade structure with appropriate folders where
you can add controllers, models and views.
So let’s go and add a new controller as shown in the below figure.
Once you add the new controller you should see some kind of code snippet as
shown in the below snippet.
Now that we have the controller we need to go and add the view. So click on
the Index function which is present in the control and click on add view menu as
shown in the below figure.
The add view pops up a modal box to enter view name which will be invoked
when this controller is called as shown in the figure below. For now keep the
view name same as the controller name and also uncheck the master page check
box.
Once you click on the ok button of the view, you should see a simple ASPX
page with the below HTML code snippet. In the below HTML code snippet I have
added “This is my first MVC application”.
If you do a CNTRL + F5 you should see a error as shown in the below figure.
This error is obvious because we have not invoked the appropriate controller /
action.
If you append the proper controller on the URL you should be able to see the
proper view.
So we will create a simple controller, attach the controller to simple
index.aspx page and view the display on the browser.
Step1:- Create project
Create a new project by selecting the MVC 2 empty web application template as
shown in the below figure.
Once you click ok, you have a readymade structure with appropriate folders where
you can add controllers, models and views.
Step 2:- Add controller
So let’s go and add a new controller as shown in the below figure.
Once you add the new controller you should see some kind of code snippet as
shown in the below snippet.
public class Default1Controller : Controller { // // GET: /Default1/ public ActionResult Index() { return View(); } }
Step 3:- Add View
Now that we have the controller we need to go and add the view. So click on
the Index function which is present in the control and click on add view menu as
shown in the below figure.
The add view pops up a modal box to enter view name which will be invoked
when this controller is called as shown in the figure below. For now keep the
view name same as the controller name and also uncheck the master page check
box.
Once you click on the ok button of the view, you should see a simple ASPX
page with the below HTML code snippet. In the below HTML code snippet I have
added “This is my first MVC application”.
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Index</title> </head> <body> <div> This is my first MVC application </div> </body> </html>
Step 4:- Run the application
If you do a CNTRL + F5 you should see a error as shown in the below figure.
This error is obvious because we have not invoked the appropriate controller /
action.
If you append the proper controller on the URL you should be able to see the
proper view.
No comments:
Post a Comment