MVC has been discussed a lot. In this article, I used a simple example to demonstrate how to use it and how to answer several frequently asked questions. I recommend that you know about MVC and try to use it for projects as much as possible.
1. Add a Controller. Right-click the Controllers directory and select a menu item.
Note: The naming rules here are suffixed with Controller.
The default generated code is as follows:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Mvc.Ajax;namespace MvcApplication1.Controllers{ public class CustomerController : Controller { // // GET: /Customer/ public ActionResult Index() { return View(); } // // GET: /Customer/Details/5 public ActionResult Details(int id) { return View(); } // // GET: /Customer/Create public ActionResult Create() { return View(); } // // POST: /Customer/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(FormCollection collection) { try { // TODO: Add insert logic here return RedirectToAction("Index"); } catch { return View(); } } // // GET: /Customer/Edit/5 public ActionResult Edit(int id) { return View(); } // // POST: /Customer/Edit/5 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection collection) { try { // TODO: Add update logic here return RedirectToAction("Index"); } catch { return View(); } } }}
2. Add a data model
3. Add a view
First, prepare a folder: Customer
Select the folder and right-click the folder and select "add" => "View"
Click "Add". The following code is displayed:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Customers>>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
Note: This page does not contain the aspx. cs file. This is also avoided by the MVC mode. Because if the page has code, it naturally contains logic, so it is not MVC. In MVC, a View, as its name implies, is only a carrier for displaying the content. Whether or not to display the content is determined by the Controller.
4. Make the view meaningful. If we want to display the top 10 Customer names with the order amount on the Index page.
First, we need to modify the Index Action.
public ActionResult Index() { Models.NorthwindDataContext context = new MvcApplication1.Models.NorthwindDataContext(); var query = from c in context.Customers let total = c.Orders.Sum( o => o.Order_Details.Sum( d => d.Quantity * d.UnitPrice)) orderby total descending select c; return View(query.Take(10).ToArray()); }
5. Run the page to see how it works
Summary:
The simple example above demonstrates how to use the MVC new development framework. It is significantly different from the traditional WebForms.
1. It is no longer based on the PostBack mechanism. The page is no longer the first thing that users get in touch with, and the page is relatively less important (at least the Page name users don't have to worry about ).
2. No ViewState is available. The reason is that the server control is no longer used on the current page. Is this a big loss? I saw it at first, but I thought it was not. This makes network development more standard.
FAQs
In fact, it is to jump to the Action. If it is on the page, Html. ActionLink is implemented through the link. If it is on the server side, the RedirectToAction method is used.
This is to add an Attribute on the Action.
[AcceptVerbs (HttpVerbs. Post)]
And the method must have a parameter: FormCollection (which represents the field in the form)
This is a standard Action and does not require any settings. The default value is GET.
On the page, use the Html. RendPartialView method. In Controller, use the PartialView method.