0

这是我的控制器代码:

    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>

}

它怎么给我错误:对象在遍历foreach循环时不包含页面类型的定义?

我想知道为什么?

编辑:

我尝试更改代码如下:

控制器:

   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);

但我得到了例外:

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

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

4

2 回答 2

2

要详细说明上面 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>

}
于 2013-03-29T16:28:33.340 回答
0

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

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);
     }
}

控制器:

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<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>

希望其他机构也能发现它有用。问题是我没有使用 WebsiteViewModel 构造函数来初始化模型对象。这终于解决了我的问题。:)

于 2013-03-30T11:04:44.567 回答