ASP. net mvc practices

Source: Internet
Author: User

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

  • Which actions are available? (10 actions listed below)

    ContentResult (Content): returns standard text
    FileContentResult (File): returns a File.
    FileStreamResult (File): returns the File stream.
    FilePathResult (File): returns the File stream.
    FileResult (File): returns a File.
    JavascriptResult (JavaScript): returns javascript and runs it on the client.
    JsonResult (Json): Return json
    PartialViewResult (PartialView): returns a local view.
    RedirectToRouteResult (RedirectToAction): Jump
    ViewResult (View): displays a View (which is the most commonly used)

  • Jump to the page or execute other actions

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.

  • Post

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)

  • How to get

This is a standard Action and does not require any settings. The default value is GET.

 

  • How to Use User Control)

On the page, use the Html. RendPartialView method. In Controller, use the PartialView method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.