这是我的控制器代码:
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循环时不包含pagetype的定义?
我想知道为什么?
编辑:
我尝试更改代码如下:
控制器:
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生成的网站表类,我的网站视图模型是冲突的。但我不明白为什么?
答案 0 :(得分:2)
要详细说明MarcinJuraszek上面的评论,您需要创建一个视图模型类来保存这些属性。
ViewModel类
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>
}
答案 1 :(得分: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构造函数来初始化模型对象。这解决了我的问题。 :)