MVC Anonymous class passing value learning and mvc Anonymous class passing
I have just been familiar with the MVC + EF framework, but I have been confused about whether or not the controller can pass anonymous data to the view. The baby said that it is very annoying to create a new entity class. It is not very troublesome to create an entity class if there is a slight difference in queries, so it is just right in the sunshine, after waking up on weekends, I tried the implementation method I had seen in the blog Park: the heroic Tuple class, the first time I admired Microsoft. Make the following record for your convenience. Don't worry about me. The baby's first blog.
First, describe the functions I want to implement: Query some data from the Controller background, and traverse the output at the front-end of the View through anonymous class storage. The initial implementation process is as follows:
Controller part:
private repairsystemEntities db = new repairsystemEntities(); // GET: TEST public ActionResult Index() { var Info = db.bom.ToList().Select(p => Tuple.Create(p.Bom_Brand, p.Bom_Model)); ViewBag.Info = Info; return View(); }
View section:
<table class="table table-hover"> <tbody> @foreach(var item in ViewBag.Info) { <tr> <td>@(item.Item1)</td> </tr> } </tbody></table>
A brief description of the Tuple class is provided as follows, all of which are from the official Microsoft documentation.
Syntax
public static Tuple<T1> Create<T1>(T1 item1)
Parameters
Item1
-
Type: T1
The value of the only component of the tuples.
Return Value
Type: System. Tuple <T1>
The value is (Item1)
Usage
// Class constructor var tuple1 = new Tuple <int> (12); // helper method var tuple2 = Tuple. create (12); // you can directly use the Console to obtain the value. writeLine (tuple1.Item1); // Displays 12Console. writeLine (tuple2.Item1); // Displays 12
Actual Example
// Create a 7-tuple.var population = new Tuple<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 2000: {1:N0}", population.Item1, population.Item7);// The example displays the following output:// Population of New York in 2000: 8,008,278
Class constructor Creation
// Create a 7-tuple.var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);// Display the first and last elements.Console.WriteLine("Population of {0} in 2000: {1:N0}", population.Item1, population.Item7);// The example displays the following output:// Population of New York in 2000: 8,008,278
Create method