Bootstrap FreeKB - MVC - Understanding Model, View, Controller (MVC)
MVC - Understanding Model, View, Controller (MVC)

Updated:   |  MVC articles

CONTROLLER

In Solution Explorer, expand Controllers, and select HomeController.cs Following is an sample HomeController.cs file for example.com. This controller will produce 3 pages:

  • www.example.com/Home/Index
  • www.example.com/Home/About
  • www.example.com/Home/Contact
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace example.com.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

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

        public ActionResult Contact()
        {
            return View();
        }
    }
}

 

VIEW

In Solution Explorer, expand View > Home, and there will be 3 razor objects:

  • Index.cshtml
  • About.cshtml
  • Contact.cshtml

 

By default, the "About" view will likely contain the following markup:

@{
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

 

With the following markup, the "About" page would look like this:

 

Even if you remove all of the markup from the "About" view, www.example.com/Home/About will still produce the following.The content displayed comes from the _Layout.cshtml and _LoginPartial.cshtml files under /View/Shared.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter d22c47 in the box below so that we can be sure you are a human.