ASP. MVC5 Learning Note 01

Source: Internet
Author: User

Since the project was also used in the development of MVC, but the specific version is not very clear, but I think the general idea is the same, but the version is high on the basis of the low version of the addition of some more convenient to operate things. Here are some of the knowledge notes I learned from ASP. NET MVC5 Advanced Programming (5), some simple operations are not documented, and some knowledge points use mind guides to write a general knowledge structure.

MVC5 is a full bin deployment, which means that when we finally release it, we simply publish the assembly of the application under the bin file, and for the server, only. NET 4.5 is available for installation.

A maxim "agreement is better than configuration"

This is in the use of MVC often hear words, do not understand what it means, now with the increase in the number of codes, for this sentence also has its own understanding, the general meaning is that according to the previous agreement, do not arbitrarily adjust the contents of the project, structure. And now the whole MVC project has been built, and if it's going to be a big project, we need to make changes to the entire underlying area. The whole structure now has a philosophy of "separating the focus of the application and making it easier for developers to develop." ”

MVC provides the result of a method call, not a dynamically generated page (script).

When using MVC in the Web, applying the MVC pattern requires that every user input in a Web application only takes the form of a request, in real-world development, each request is routed to a method on the controller, which is fully responsible for interpreting the requests, potentially manipulating the model, Then select a view to feedback to the user.

The MVC5 between the controller and the view is worth passing

About the transfer of values we know there are viewbag,viewdata,viewdatadictionary, models (strongly typed) and other forms of transmission,

In fact, the value is passed through the viewdatadictionary, the data from the controller to the view is through a viewdatadictionary named ViewData (A dictionary Class), there is ViewBag is viewdata dynamic wrapper;

A dynamic value cannot be passed as a parameter to an extension method, because the C # compiler must know the true type of each parameter at compile time in order to choose the correct extension method.

With strongly typed model objects, you must make all the data strongly typed so that you can use IntelliSense in the view.

In the case of a generic class, you need to use ienumerable<> to specify it in the view.

     @model ienumerable<registerexternalloginmodel>

If the data passed by the controller is itself a IEnumerable or a type that inherits it, then it only needs to write

     @model Registerexternalloginmodel

can come out.

As for saying viewdata["" "and ViewBag. The use method is simple, but it is recommended to use less, especially in some large projects, we can completely encapsulate the data into the model, so that the model between the Controller and the view of the data transfer.

Important points of knowledge in @Razor view
    1. : Write C # code in the view with "@", and the compiler will view it as a background language to compile and run.
    2. : It is possible to differentiate some common two semantics by ().
    3. : @@ 两 escapes for an @ sign;
    4. : You can use @{} to differentiate blocks of code;
    5. : @Html. Raw (): Encode output in Html format;
    6. : @Html. RenderPartial ("Indx"); This area places a partial view, without the controller, to pass the model to it, so that the model in the detail view is passed by us, and the detail view cannot specify the layout. It can only be said to be nested in the same way as before.
    7. : @Hmtl. Renderaction (); This is different from the above, it needs to be handled by the Controller;
    8. : @* annotation *@, annotation notation in this view;
    9. : Call the generic method <> need to be aware that you can use @ () to differentiate.
    10. : @Html. Renderbody (), this is defined in the layout, and finally, the Web page is nested here.
    11. : @Html. Rendersection ("Footer"); Set the section below it to display it in its web page.
    12. : @Html. Displayfor (); the output of the view takes the Html helper method.
    13. : @Html. DropDownList (); drop-down list. SelectList () is set here;
        <div id= "Body" >            @RenderSection ("featured", Required:false)            <section class= "Content-wrapper Main-content Clear-fix ">                @RenderBody ()            </section>        </div>

@section Featured {<section class= "featured" > <div class= "Content-wrapper" > 

This self-defined node replaces our @html.rendersection () in Layout view; @section syntax specifies the content for a node defined in the layout.

If you want to display an HTML tag, you return an instance of the System.Web.IHtmlString object.

For complex applications, we want to explore different architectures and design patterns to meet our needs, domain driven design (DDD) is a way for teams to handle complex applications, command query segregation (CQRS) is also a team development model, It occupies a major share in the development of complex applications.

Introduction to include () when querying data
        Public ActionResult edites ()        {            var albums = Db.albums.Include (A = = a.article). Include (a=>a.genre);            Return View (albums. ToList ());        }

In the case of the above, this include, the data taken from the database is preloaded, which means that all of the data can be loaded using the query statement as much as possible. There is also a lazy load, which the EF loads only the primary object data in the LINQ query, not the objects that populate our settings. One drawback is that each time you need to load one more, this means that we need to add the virtual tag before declaring the class object.

The Include method here can be seen as an optimization to reduce the query data needed to build the complete model.

Model binding

Before just know in the formal parameter transfer model (class), we do not need to control how the data come over, the underlying architecture has helped us to encapsulate, but now read only to find the previous understanding of the fur, there is no point of knowledge. This is a validation of the model before validating the model binding, and modelstate.isvalid () is doing this to verify the service side, which is preceded by a model-bound thing. ([attribute in the Include= class ",)]);

Updatamodel (); TryUpdateModel (); These two are also model binding validated, in front. The second returns a Boolean type value.

ASP. MVC5 Learning Note 01

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.