MVC Learning Notes One

Source: Internet
Author: User

Found some MVC information in the garden. Through the demo to strengthen the study of MVC knowledge points, summed up a few points:

One, Access way: Http:ip (localhost):p ort/controllername/methodname?argname

The bindings in C # and the display of variables in aspx/cshtml:

1, C#:viewbag.name=value/method; aspx/cshtml: @ViewBag. Name

2, c#:viewdata["Name"]=value/method; aspx/cshtml:@ (@ViewData ["name"] As String)//Type conversion

Third, the corresponding view is generated by the controller's method name, and the name of the view and the controller should preferably correspond.

Four, in the model Declaration object's property corresponds to the database field, but also can declare the field type such as: [key]=] "PRIMARY key", [required]= "must fill etc."

V. By reference of using System.Data.Entity; Namespaces allow model entity objects to inherit: DbContext.

Declare objects inside the entity object class such as: Public dbset<student> stulist {get; set;}

VI. Create a view to change the name, select the corresponding model class, reference footsteps, action template such as: CRUD, whether it is a distribution view, the selected template and the occupied content page corresponds to the location.

Vii. using the Entity Framework codefrist to generate corresponding database tables and database names,

The database name consists of the space name and the class name of the object entity, such as: Mvc3Demo.Models.StudentEntities

Viii. 2 tables, such as class and student tables, are linked by LINQ: var stulist = db.stulist. Include (s = s.scclass);

Nine, the drop-down menu binding method one:

ViewBag. Foreign Key id=new SelectList (Association class collection, "ID of association Class", "Name to display in association class", "current class foreign key ID used as selected value")

Page Call method: @Html. DropDownList ("Foreign Key ID", String.Empty)

Drop-down menu binding method two:

ViewBag. Foreign Key Id= Association class collection. (M=>m.columnname). ToList ();

Page call: @Html. DropDownList ("Foreign Key ID", New SelectList (ViewBag. Set as

System.Collections.IEnumerable, "id", "ColumnName", Model. Foreign key ID))

Ten, through the generation of crud we can see the edit method one of which has [HttpPost], just modified after the form submitted post method of processing,

The one that is not, is the method of modifying the previous Get mode query.

Note: Common data annotations are as follows: Excerpt from: a Netizen

[Display (name = "name")]//displayed name
[Required (errormessage = "You need to fill in {0}")]//must field error prompt
[Stringlength (minimumlength = 3)]//Set maximum length and minimum length
[Range (1, errormessage = "age is not filled in correctly!) ")]//Set Value range
[Range (typeof (decimal), "50.00", "250.00", errormessage = "Height exceeds specified range")]
[DataType (datatype.date, errormessage = "{0} malformed")]//Set data type and error prompt
[DataType (Datatype.multilinetext)]//Multiline text
[RegularExpression (@ "(\w) + (\.\w+) *@ (\w) + ((\.\w+) +)", errormessage = "{0} format incorrect")]//regular validation
[DataType (Datatype.password)]//password verification
[DataType (datatype.emailaddress, errormessage = "{0} format is incorrect")]//mailbox Format verification
[Compare ("Email", errormessage = "{0} two input inconsistent")]//set to compare values of two fields (custom)
[Remote ("Checkphone", "stuinfo", errormessage = "{0} registered")]//Validate data in channel (route) in the specified Conteroller [custom]

* Page verification methods such as: @Html. validationmessagefor (model = model.age). The controller code is attached:

  Private Studententities db = new studententities ();  GET:/studentmanager/public ViewResult Index () {var stulist = Db.stuList.Include (s            = S.scclass); Return View (stulist.        ToList ()); }///GET:/STUDENTMANAGER/DETAILS/5 public ViewResult Details (int id) {Student            Student = Db.stuList.Find (ID);        Return View (student); }///GET:/studentmanager/create public ActionResult Create () {Viewbag.scclassi            D = new SelectList (db.scclasslist, "ID", "className");        return View ();        }////POST:/studentmanager/create [httppost] public actionresult Create (Student Student)                {if (modelstate.isvalid) {DB.STULIST.ADD (student); Db.                SaveChanges ();                    Return redirecttoaction ("Index"); } Viewbag.scclassid = new SelectList (db.scclasslist, "id", "className", Student.scclassid);        Return View (student); }///GET:/STUDENTMANAGER/EDIT/5 public actionresult Edit (int id) {Student stud            ENT = db.stuList.Find (ID);            Viewbag.scclassid = new SelectList (db.scclasslist, "ID", "ClassName", Student.scclassid);        Return View (student);        }////POST:/STUDENTMANAGER/EDIT/5 [httppost] public actionresult Edit (Student Student) {if (modelstate.isvalid) {db. Entry (student).                state = entitystate.modified; Db.                SaveChanges ();            Return redirecttoaction ("Index");            } viewbag.scclassid = new SelectList (db.scclasslist, "ID", "ClassName", Student.scclassid);        Return View (student); }///GET:/STUDENTMANAGER/DELETE/5 public actionresult Delete (int id) {Student Student = Db.stuList.Find (ID);        Return View (student); }////POST:/STUDENTMANAGER/DELETE/5 [HttpPost, ActionName ("Delete")] public ActionResult De            leteconfirmed (int id) {Student Student = db.stuList.Find (ID);            Db.stuList.Remove (student); Db.            SaveChanges ();        Return redirecttoaction ("Index"); }

  

MVC Learning Notes One

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.