MVC reading Notes

Source: Internet
Author: User
Tags http post

Razor was introduced at the time of MVC3.
Top items in the Mvc4
/controllers save those controller types that handle URL requests
/models preserves those representations and operating figures, as well as the business
/views save those UI template files that are responsible for the results
/scripts Save the JavaScript library file and the foot book (JS)
/images Save the image used by the station Dot
/content save CSS and other site content
/filters saved the discard code. The discard device is a high-performance
/app_data Save the files you want to read/write into
/app_start Save configuration codes for some features, such as routing, bundling kidnapped, and Web API


MVC's working system
The URL first tells the routing machine which controller to instantiate, which operation method to use, and provides the required parameters for the method. Then the Controller's method decides which video to use and renders the image.
How routing URLs map to operations
Video as a template generates a string that is returned to the browser (usually in HTML format)





Viewbag.message equivalent to viewdata["message"]
@@ 符 for a @
@section language is defined as the content of a
The Rendersection method has a reload version that allows you to specify unwanted episodes. can be transferred to required's parameters a false in the label xx is optional.
Egg: First @<section xx{this is the &LT;STRONG&GT;FOOTER&LT;/STRONG&GT;}//here XX means a
Refer to the section, @RenderSection ("xx", Required:false)//This indicates that the XX section is referenced. If XX nodes exist.
_viewstart.cshtml, the Bureau of the tacit announcement
Specify some of the images
Egg:public ActionResult Message ()
{
Viewbag.message= "This is a partial view";
Return Partialview ();
}
In this case, the image message.cshtml is rendered. If the _viewstart.cshtml page is specified (rather than in the video), it cannot be rendered. In addition to being unable to specify the layout, some of the images look and normal view does not have any area.



The ASP. NET MVC4 project will automatically include references to the physical framework. EF is an image-relational mapping framework.



The HTML helper method was developed for the help of HTML.
About the use of HTML helper methods.
Html.BeginForm Help method send an HTTP POST request to the current URL.
Html.validationsummary helper methods can be used to display all the modelstate in the dictionary.
Html.textbox helper methods renders a type attribute as the text input tag.
The Html.textarea method is a similar method of assistance to Html.textbox.
Html.label Help method is a way of showing
Html.dropdownlist and Html.listbox methods all return a <select/> element.
Html.validationmessage helper methods when a particular field in the Modelstate dictionary is wrong, you can use the Validationmessage helper method to display the corresponding bug.
Routes. MapRoute (//Setting the default route value
"Default",//route name
' {controller}/{action}/{id} ',//URL with parameter
New {controller = "Home", action = "Index", id = urlparameter.optional}//parameter default value
);



1.[required] Required
2. Anonymous Anonymous
3.StringLength egg:[stringlegth (160,minimumlength=3)]
4.RegularExpression egg:[@ "[a-za-z0"]
5.Range Egg:range (5,40)
6.

Authorize grants authority to entrust
Egg
Namespace Mvcapplication3.controllers
{

public class album
{
private int Album;

public int Album1
{
get {return Album;}
set {Album = value;}
}
private string Title;

public string Title1
{
get {return Title;}
set {Title = value;}
}
}

public class Sdycontroller:controller
{
//
GET:/sdy/

Public ActionResult Index ()
{
var a = Getalbums ();
Return View (a);
}
[Authorize]
Public ActionResult Buy (int id)
{
var buys = Getalbums (). Single (a = A.ALBUM1 = = ID);
Return View (buys);
}

private static list<album> Getalbums ()
{
var albums = new List<album> {
New album{album1=1,title1= "Number One"},
New album{album1=2,title1= "Number of"},
New album{album1=3,title1= "Number three"}
//};

var albums = new list<album> ();
var Ablumss = new album ();
Ablumss. ALBUM1 = Convert.ToInt32 (2);
Ablumss. title1= "Number One";
Albums. ADD (ABLUMSS);
Ablumss. ADD (1, "number One");
Ablumss. ADD (2, "number");
Ablumss. ADD (3, "number three");
return albums;
}
}
}

This is the index page
@model list<mvcapplication3.controllers.album>

@{
Viewbag.title = "Index";
}

<ul>
@foreach (var a in Model)
{
<li>
@a.album1
@a.title1
@Html. ActionLink ("Buy It! "," Buy ", new {ID=A.ALBUM1})
<a href= "@Url. Action (" Buy "," Sdy ", new {ID=A.ALBUM1})" > Buy a bar </a>
</li>
}
</ul>


This is Buy page
@model MvcApplication3.Controllers.album

@{
Viewbag.title = "Buy";
}


@Model. Album1
OK can be purchased!

When you add a [authorize] to a buy, you can intelligently determine if the user is in a landing state, and if it is not on the landing, it will automatically jump to the landing page. If you are on a landing, go directly to the landing page.
If [authorize] is removed, it jumps directly to the specified page.


[Authorize (roles= ")]//granted to certain user


Rewrite and route to Rul than
URL rewrite is a URL that maps to another address.
The route-related note is how to map URLs to resources.
Each ASP. NET MVC application requires at least one route to define the way in which it is being asked, and there will always be one or more routes in the program. A complex application might contain 10 or more routes.
A method called RegisterRoutes is used in the Application_Start method. The method in the ~/app_start/routeconfig.cs file we can use to register all the routes needed for the application.
For example,/albums/display/123 's request would cause MVC to instantiate the Albumscontroller controller and adjust its display method to inform the 123 of the display method.
The routing order of the route is from the top down.
EGG:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using System.Web.Routing;

Namespace MvcApplication3
{
public class Routeconfig
{
public static void RegisterRoutes (RouteCollection routes)
{
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");//Ignore the URL of this rule

Routes. MapRoute (Name: "Default5",
URL: "{controller}/{action}/{*id}",
defaults:new {controller = "Home", action = "Index"}
);

Routes. MapRoute (Name: "Default4",
URL: "{controller}/{action}",
defaults:new {controller = "Home", action = "Index"}
);
Routes. MapRoute (Name: "DEFAULT3",
URL: "{controller}/{action}/{parma1}/{parma2}",
defaults:new {controller= "", Action= ""}
);

}
}
}

MVC reading Notes

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.