ASP.Net MVC

champak
₹2,000.00 ₹300.00
  • 11 students
  • 6 lessons
  • 0 quizzes
  • 10 week duration

ASP.Net MVC

How to create a controller in ASP.Net MVC?

With this post we begin a series of posts on ASP.Net MVC.
What is MVC?
MVC stands for Model View Controller architecture.
What is the Model? The Model stores data.
What is the View? The View provides the representation. In our case it would be HTML, CSS etc.
What is the Controller? The Controller processes the user input. To put it plainly, it is the place where you would handle the get or post method and deliver a response.

We start our MVC series by developing a few controllers.
1. Download and install Visual Studio 2017. It is currently available at–
https://visualstudio.microsoft.com/vs/community/

Run the installer and select ASP.NET and web development as the component that you want to install. Run the installer and finish.

Start a new project in Visual Studio.

From the list, please select
ASP.Net web application.

Select MVC and press OK.

After creating the project,
Look up the open project in Visual Studio.

Now, we shall add a controller. Right click on the Controller folder and

Select empty MVC 5 Controller.

By default Visual Studio will propose the name DefaultController. Change it to ChampakController. ChampakController will become the name of the new class while Champak will become part of the URL.
This is the controller as created by Visual Studio.

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCOne.Controllers
{
public class ChampakController : Controller
{
// GET: Champak
public ActionResult Index()
{
return View();
}
}
}

Delete the Index function and create a new function called GetStringData.

 

public string GetStringData()
{
return "This is a string from Get String Data";
}

 

Type the following URL in the browser. http://localhost:49455/Champak/GetStringData and view the result.

*Your port no could be different.

 

Next, we will create a class and return an object of the class.

Consider the following code.
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCOne.Controllers{
public class Book
{
private string bookname, author;
private int price;
public Book(string bookname, string author, int price)
{
this.bookname = bookname;
this.author = author;
this.price = price;
}
public string BookName
{
get{
return bookname;
}
}
public string Author
{
get
{
return author;
}
}
public int Price
{
get
{
return price;
}
}
public override string ToString()
{
return string.Format("Name={0}, Author={1}, Price={2}",bookname,author,price);      }
}
public class ChampakController : Controller
{
public string GetStringData()
{
return "This is a string from Get String Data";
}
public Book GetBook()
{
Book b = new Book("Basic C", "Rishabh", 250);
return b;
}
}
}

Run the program and you get its toString method output in the browser.

We will add a view in the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *