Learn MVC from scratch (i)

Source: Internet
Author: User
Tags actionlink

In fact, in school, has opened the course of MVC, teaching materials by the teacher himself, is similar to the Microsoft Music Store a bookstore project, at that time can not understand Linq, Lambda , only remember is in accordance with the teacher's method, copy + paste, unclear its meaning, also do not know its rationale. There is an impression in the mind, and later in the post-internship (GIS) geographical mapping of the development and maintenance, until the change of work to become a positive, while the development as a technical reserve for the opportunity to learn from the beginning of MVC

MVC is a high-level architectural pattern, and because of the web Form that was first studied, the separation of the MVC display, which was just beginning with total sleep, increased the complexity of the application and was not useful. It was really sighted. No insight into -the power of the model-view-controller . Layered, concise and easy to maintain

model : A set of entity classes that can be understood as a "data layer."   View : Defines the interface that the user displays and is specified by the controller. Controller : Access guide to handle requests from users, as well as the entire application flow of communication

The following is a simple table operation, the last of which is as follows:

About this demo, in three parts of the introduction:

1.CSS type Reference

① in MVC, if you reference a template page, you see @Styles in the Default Template page. Render ("~/content/css"), here is a reference to the CSS style configuration source, found in the folder App_start BundleConfig.cs, click to find

Bundles. ADD (new stylebundle ("~/content/css"). Include ("~/content/site.css"));

Right here is the referenced CSS stylesheet, if you need to refer to the Bootstrap.min.css style at this point, directly with the Include ("", "",... ), just note that this CSS file is not recognized when the browser is compiled, and only the Min is removed , such as renaming Bootstrap.css to

② at this point, the browser compiled interface is as follows

2. Pagination Control

① Download and install the NuGet package for open source paging controls, referencing the control's namespace using WEBDIYER.WEBCONTROLS.MVC in the Controller interface;

② also reference namespaces in the View interface and reference @model pagedlist< entity class names (table names) >

Note: The view interface allows only one @model reference, if you previously chose to create a strongly typed view, you need to comment on this strongly typed section and then add the @model project name. Models

3. Delete the check

Additions and deletions are the most fundamental to the operation of the data, in the final analysis, it is still inseparable from the transfer of parameters, to edit as an example, the current desk click Edit, pass the student's ID, and submit to the set controller, the controller receives parameters, to judge, and then return to the editing interface view, editing interface Click Save, Passing the student object to the controller, the controller then completes the operation of the data according to LINQ and returns to the main view

Edit:

① in the view page of the main interface, the student ID of the currently selected line is passed under Edit function,

Jump format: @Html. ActionLink ("Connection text", "method", "parameters")
@Html. ActionLink (" editor " "edit"new {Id=item. STU_ID})

② under the corresponding controller, the receiving view passes in the student ID, makes the judgment, if has the value, then returns to the edit view

       //Global Variables for the image        PrivateIbbcentities db =Newibbcentities (); //        //GET:/DEFAULT1/EDIT/5         PublicActionResult Edit (intID) {Students Students=db.            Students.find (ID); if(Students = =NULL)            {                returnHttpnotfound (); }            returnView (students); }

③ in Edit view, add the Submit button <input type= "Submit" value= "Save"/>

How to set the Post request @using (Html.BeginForm ("Edit", "Default", FormMethod.Post)) {<></> }

Where Html.BeginForm ("Method name", "Controller name", "Request Method")

It can also be written as @using (Html.BeginForm ()) { @Html. ValidationSummary (True) <></> ...}

This notation, using the default method (automatically submitted to the corresponding controller under the same (class) name and with the [HttpPost] method, and the submission method is post)

④ in the background controller, using LINQ to manipulate the data, and still return to the main view when finished

        [HttpPost]        public  actionresult Edit (Students Students)        {            if (modelstate.isvalid)            {                = entitystate.modified;                Db. SaveChanges ();                 return Redirecttoaction ("Index");            }             return View (students);        }

Make a note of editing, as long as a little bit about Linq, it is not difficult to think of creating, deleting, viewing methods

For the deletion, pass the ID, that is, according to the ID (primary key) to perform the delete operation, the idea: Query the student object of the ID submitted by the view, then delete the object, save

        // POST:/DEFAULT1/DELETE/5        [HttpPost, ActionName ("Delete")]          Public ActionResult deleteconfirmed (int  ID)        {            = db. Students.find (ID);            Db. Students.remove (Students);            Db. SaveChanges ();             return Redirecttoaction ("Index");        }

For creation, first verify that the object being added is reasonable and reasonable: add, save.

       // POST:/default1/create         [HttpPost]        public  actionresult Create (Students Students)        {            if  (modelstate.isvalid)            {                db. Students.add (Students);                Db. SaveChanges ();                 return Redirecttoaction ("Index");            }             return View (students);        }

Right as a learning record

Learning mvc from scratch (i)

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.