How to create a simple ASP.NET MVC program?
In this Exercise we will create a sample 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.
Step1:- Create project
Create a new project by selecting the MVC 2 empty web application template as shown in the below figure
After click ok button then we get structure with appropriate folders where we 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyFirstMvcProg.Controllers
{
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyFirstMvcProg.Controllers
{
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”.
Step 4:- Run the application
If you do a CNTRL + F5 You get error 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