5.MVC Framework Development (strong-type development, controller-to-interface transfer of data to several methods)

Source: Internet
Author: User

The form element name in the interface form is mapped to the field name of the database table one by one (which table's data is the model of that table)

In the view page, you can specify which model the page belongs to

Note: The above relationships can be developed through MVC's strongly-typed view to address the IntelliSense of our development code

1. Steps for strongly-typed view development

1) Specify a strongly typed type (must be a reference type) in the page, code: Example:

@model list<mymvcbookshop.models.book>

2) from the controller to the strong type of data to the view view, then you must ensure that the incoming data will be consistent with the type defined in the strongly typed view, for example:

Mybookshopcontext bookshop = new Mybookshopcontext (); Public ActionResult Index ()        {            list<book> m = bookshop. Books.orderby (p = p.id). Take (16). ToList ();            Return View (m);        }

3) In the View page, apply its Model property, for example:

@{foreach (var item in Model) {<li><a href= "/book/[email protected]" > @item. Title</li>}}

2. Several methods for transferring data to the interface of controller

1) pass through strongly typed

2) pass through the ViewBag, it is the dynamic parsing type, at run time will automatically parse what type, when writing does not know the type

3) passing through ViewData, it is a collection of ways to store (in the form of key-value pairs stored), in the interface using the first conversion after use, for example:

In the controller Mybookshopcontext Bookshop = new Mybookshopcontext ();        [HttpGet]        Public ActionResult Index ()        {            list<category> listcstegory = bookshop. Categories.orderby (p = p.id). ToList ();            viewdata["Category"] = listcstegory;            return View ();        } On the view page <ul class= "Black" >                @{    list<category> listcate = viewdata["Category"] as list< category>;    foreach (var item in listcate)    {                        <li><a href= "/book/list/@item. Id "> @item. Name</a></li>    }}</ul>

4) Pass data via TempData (typically used primarily for data transfer between different actions), TempData only once, and store data in key-value pairs

The return value is: Return Redirecttoaction ("Another action name") so that TempData can only read the data once

Summarize the difference between ViewBag,ViewData,tempdata ?

5.MVC Framework Development (strong-type development, controller-to-interface transfer of data to several methods)

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.