我有一个来自数据库的类构建,名为RSS_Head
。它的一个属性是rss-feed的url。我想在表格中显示链接。
@using Pgsrssreader.Models
@model Pgsrssreader.Models.RSS_Head
<table>
@{RSS_Head rsshead = new RSS_Head();}
<tr>
<th>@Html.LabelFor(model => rsshead.SUrl)</th>
</tr>
现在我尝试使用foreach循环来显示这样的URL:
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.SUrl)
</td>
</tr>
</table>
我收到错误:
Foreach语句无法对
Pgsrssreader.Models.RSS_Head
类型的变量进行操作,因为Pgsrssreader.Models.RSS_Head
不包含GetEnumerator的公共定义
namespace Pgsrssreader.Models
{
using System;
using System.Collections.Generic;
public partial class RSS_Head
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public RSS_Head()
{
this.RSS_Row = new HashSet<RSS_Row>();
this.Rss_Category = new HashSet<Rss_Category>();
}
public int iRSSid { get; set; }
public string SUrl { get; set; }
public string sTitle { get; set; }
public string sLink { get; set; }
public string sDescription { get; set; }
public string sLanguage { get; set; }
public Nullable<System.DateTime> dBuildDate { get; set; }
public string sCopyright { get; set; }
public string sImageUrl { get; set; }
public string sImageTitle { get; set; }
public string sImageDescription { get; set; }
public Nullable<int> iImageHeight { get; set; }
public Nullable<int> iImageWidth { get; set; }
public Nullable<bool> bAktiv { get; set; }
public Nullable<byte> iDatumKorr { get; set; }
public Nullable<bool> bDatumFinns { get; set; }
public Nullable<byte> iTableIndex { get; set; }
public string sTidning { get; set; }
public string sCatFieldName { get; set; }
public string sCatName { get; set; }
public string sDateFieldName { get; set; }
public string sDateReplace { get; set; }
public string sRegexp { get; set; }
public Nullable<int> iPaperId { get; set; }
public Nullable<int> iFailed { get; set; }
public Nullable<bool> IsDeleted { get; set; }
public virtual RSS_Papers RSS_Papers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<RSS_Row> RSS_Row { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Rss_Category> Rss_Category { get; set; }
}
}
编辑:解决方案 我最终在共享视图中创建了一个帮助类:
public class Rsshelper
{
private Rss_DevEntities _db = new Rss_DevEntities();
public List<RSS_Head> Feeds()
{
List<RSS_Head> rss_head = new List<RSS_Head>();
rss_head = _db.RSS_Head.ToList();
return (rss_head);
}
}
然后我写了
@{
List<RSS_Head> rsslist = new List<RSS_Head>();
Rsshelper rsshelper = new Rsshelper();
rsslist = rsshelper.Feeds();
}
<table>
@{if (rsslist != null)
{
foreach (var item in rsslist)
{
<tr>
<td>
@item.SUrl
</td>
</tr>
显示网址。可能有一个更好的解决方案,所以我可以随意改进:)
答案 0 :(得分:1)
主要是实体框架是此类操作的最佳方法 首先获取列表中的数据库记录。你可以在你的模型中提到这个,
型号:
public IEnumerable<modelname> AnyName{get; set;}
逻辑代码(控制器):
Public ActionResult Youaction()
{
modelclass yourmodel= new modelclass();
yourmodel.AnyName=Fun1();
}
public IEnumerable<modelName> Fun1()
{
databaseclass dbobj1= new databaseclass();
tableclass tbl= new tableclass();
modelName yourmodel= new modelName();
return(var v in dbobj1.table1 select new model
modelproperty1=v.tablevalue1,
.,
.,
modelpropertyforUrl=v.tablevalueForUrl
).ToList();
}
您的观点:
<table>
@foreach(var item in AnyName)
{
<tr>
<td>@item.modelpropertyforUrl</td>
</tr>
}
</table>