asp.net MVC3 controller passes anonymous object to view instance

Source: Internet
Author: User

asp.net MVC3 + Entity Framework project, passing anonymous objects from the controller to the view is very common, originally thought dynamic can be easily done, finally found I was wrong:

Controller:

The code is as follows Copy Code


Public ActionResult Index ()
{
TestContext context = new TestContext ();
Dynamic Data = context. People
. Join (context. Pets, person => person. Id, Pet => pet.pid, (person, Pet) => new {person = person. Name, pet = pet. Name});
return View (data);
}

View:


@model Dynamic

@foreach (var item in Model)
{
@ (item. person) <text>,</text>@ (item. Pet) <br/>
}

The reason for this is that the C # compiler always compiles anonymous types into internal, and throws an exception when a member that is not visible in the current context is accessed through dynamic. Problem Recurrence:

The code is as follows Copy Code

Using System;
Using System.Collections.Generic;
Using System.Linq;

Namespace Controller
{
public class Test
{
<summary>
Impersonate an anonymous object anonymous class
</summary>
Private Class Anonymous
{
public string Person {get; set;}
public string Pet {get; set;}
}

Public dynamic GetValue ()
{
return new Anonymous ();
}
}
}

Namespace View
{
Using Controller;

Class Program
{
static void Main (string[] args)
{
Dynamic anonymous = new Test (). GetValue ();
Console.WriteLine (anonymous. person);

Console.readkey ();
}
}
}

Used to solve the old Zhao's Todynamic method, today in the System namespace of the. NET 4.0 to see a class tuple, understand that it can also solve the problem above:

Controller:

  code is as follows copy code

Public ActionResult Index ()
{
TestContext context = new TestContext ();
Dynamic Data = context. People
. Join (context. Pets, person => person. Id, Pet => pet.pid, (person, Pet) => new {person = person. Name, pet = pet. Name})
. ToList (). Select Item => tuple.create (item. Person, item. Pet));
return View (data);
}

View:


@model ienumerable<tuple<string, string>>

@foreach (var item in Model)
{
@ (item. ITEM1) <text>,</text>@ (item. ITEM2) <br/>
}

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.