Asp.net mvc 3 如何循环通过lambda表达式返回的列表?

Asp.net mvc 3 如何循环通过lambda表达式返回的列表?,asp.net-mvc-3,linq-to-sql,razor,Asp.net Mvc 3,Linq To Sql,Razor,这是我的控制器代码: var myquery = mycontext.Websites.Select(x => new {x.pagetype,x.website1,x.creationdate,x.expirationdate,x.domainregistrar,x.area,x.pin ,x.city, age = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today)

这是我的控制器代码:

    var myquery = mycontext.Websites.Select(x => new {x.pagetype,x.website1,x.creationdate,x.expirationdate,x.domainregistrar,x.area,x.pin
                    ,x.city, age = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today)
                });

return View(myquery);
这是我的视图代码:

@foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.pagetype </span>

</td>

</tr>

}
但我有个例外:

操作可能会在运行时破坏操作的稳定性。确保应用程序没有加载两个相互冲突的类库版本


我所能想到的只是linq为网站表生成的类,而我的Websiteview模型是冲突的。但是我不明白为什么?

要详细说明上面MarcinJuraszek的评论,您需要创建一个视图模型类来保存这些属性

视图模型类

public class MyViewModel{
   public string Pagetype {get;set;}
   public string Website1 {get;set;}
   public DateTime Creationdate {get;set;}
   public DateTime Expirationdate {get;set;}
   public string Domainregistrar {get;set;}
   public string Area {get;set;}
   public string Pin {get;set;}
   public string City {get;set;}
   public int Age {get;set;}
}
现在,在查询中,选择进入ViewModel类

var model = mycontext.Websites.Select(x => new MyViewModel{
                  PageType = x.pagetype
                  WebSite1 = x.website1
                  CreationDate = x.creationdate
                  ExpirationDate = x.expirationdate
                  DomainRegistrar = x.domainregistrar
                  Area = x.area
                  Pin = x.pin
                  City = x.city,
                  Age =DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today)
               }).ToList();

return View(model);
现在,你需要强烈地输入你的视图,你就可以开始了

@model IEnumerable<MyViewModel>

@foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.Pagetype </span>

</td>

</tr>

}
@model IEnumerable
@foreach(模型中的var项目)
{
@项目.页面类型
}

一整天之后,我可以像下面这样解决它:

public WebSiteViewModel
{
     string PageType { get; set;}
     string WebSite { get; set;}
     DateTime CreationDate { get; set;}
     DateTime ExpirationDate { get; set;}
     string DomainRegistrar { get; set;}
     string Area { get; set;}
     string Pin  { get; set;}
     string City { get; set;}
     DateTime Age {get; set;}

     //Constructor
     public WebSiteViewModel(YourWebSiteEntity w)
     {
             PageType = w.pagetype;
             WebSite = w.website1;
             CreationDate = w.creationdate;
             ExpirationDate = x.expirationdate;
             DomainRegistrar = x.domainregistrar;
             Area = x.area;
             Pin = x.pin;
             City = x.city;
             Age = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today);
     }
}
控制器:

   CRMDataContext mycontext = new CRMDataContext();
                var myquery = mycontext.Websites.Select(x => new WebsiteViewModel
                {
                    pagetype = x.pagetype,
                    site = x.site,
                    creationdate = x.creationdate ?? DateTime.Today,
                    expirationdate = x.expirationdate ?? DateTime.Today,
                    domain_registrar = x.domainregistrar,
                    Area = x.area,
                    pin = x.pin,
                    City = x.city,
                   difference = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today).ToString()
                }).ToList();

     return View(myquery);
public ActionResult Index()
        {
            CRMDataContext mycontext = new CRMDataContext();


            var myquery = mycontext.Websites.Select(x => new WebsiteViewModel(x.pagetype,
               x.site, x.creationdate ?? DateTime.Today, x.expirationdate ?? DateTime.Today, x.domainregistrar, x.pin, x.area, x.city, "Abc")).ToList();

            return View(myquery);
        }
视图:

@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
页面类型
网站
创建日期
到期日期
差异
域名注册商
别针
地区
城市
@foreach(模型中的var项目)
{
@item.pagetype
@item.site
…等等
}

希望其他人也能觉得它有用。问题是我并没有使用WebsiteViewModel构造函数初始化模型对象。这至少解决了我的问题。:)

无法将匿名类型从控制器返回到视图。你必须创建一个模型类并返回它,这给了我另一个错误。请在我的帖子中检查我的编辑。
@model IEnumerable<ImportFromExcel.Model.WebsiteViewModel>
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<table>
    <tr>
        <th>
            Page type
        </th>
        <th>
            Website
        </th>
        <th>
            Creation date
        </th>
        <th>
            Expiration Date
        </th>
        <th>
        difference
        </th>
        <th>
            Domain Registrar
        </th>
        <th>
            Pin
        </th>
        <th>
            Area
        </th>
        <th>
            City
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.pagetype </span>
            </td>
            <td>
                <span>@item.site </span>
            </td>
..so on
        </tr>

    }
</table>