MVC Anonymous Class Transfer value learning

Source: Internet
Author: User

Shortly after contacting the MVC+EF framework, it is always confusing whether the controller can and how to pass anonymous class data to the view. Baby said it is very annoying to create a new entity class, query a little different to build an entity class is not very troublesome, so take advantage of the sun, the weekend sleep to natural wake up after trying to have been in the blog park to see the realization of the way: the wise Shenwu of the tuple class, the first time to Microsoft admiration. So make the following record, convenient to use after yourself. The great God does not squirt me, the first time the baby writes the blog.

First describe the function I want to implement: from the controller background query some data, through anonymous class storage, the view front-end traversal output . The original implementation process is as follows:

Controller section:

      Private Repairsystementities db = new repairsystementities ();        Get:test public        ActionResult Index ()        {            tuple.create (P.bom_brand, P.bom_model));            Viewbag.info = Info;            return View ();        }

View section:

<Tableclass= "Table Table-hover">    <tbody>@foreach (var item in viewbag.info) {<TR>            <TD>@ (item. ITEM1)</TD>        </TR>    }    </tbody></Table>

The following is a simple description of the tuple class, all from Microsoft Official documents, address

Grammar
public static tuple<t1> create<t1> (T1 item1)
Parameters

Item1

Type: T1

The value of the tuple's only component.

return value

Type: system.tuple<t1>

Tuple, whose value is (item1)

How to use
// class Constructors var New tuple<int> (n); // Helper Methods var tuple2 = tuple.create (n); // The get value method takes directly Console.WriteLine (Tuple1. ITEM1);     // Displays Console.WriteLine (Tuple2. ITEM1);     // Displays
Practical examples
//Create a 7-tuple.varPopulation =Newtuple<string,int,int,int,int,int,int>(                           "New York",7891957,7781984,                            7894862,7071639,7322564,8008278);//Display the first and last elements.Console.WriteLine ("Population of {0} in: {1:n0}", population. Item1, population. ITEM7);//The example displays the following output://Population of New York in 2000:8,008,278
class constructor creates
//Create a 7-tuple.varPopulation = tuple.create ("New York",7891957,7781984,7894862,7071639,7322564,8008278);//Display the first and last elements.Console.WriteLine ("Population of {0} in: {1:n0}", population. Item1, population. ITEM7);//The example displays the following output://Population of New York in 2000:8,008,278
Create Method

MVC Anonymous Class Transfer value learning

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.