循环:asp.net MVC 3视图通过模型用foreach
@model IEnumerable<AJA.Models.DB.Article>
所有我想要做的是遍历模型做一些逻辑:
@foreach (var item in Model)
{
var article = item.article1;
if (article.Length > 500)
{
article = article.Substring(0, 500) + "...";
}
然而,当我执行搜索,错误:
exception: System.NullReferenceException: The reference of the object is not defined
to an instance of the object.
如果我做@Html.DisplayFor(modelItem => item.article1)
独自一人,我得到的文章好,但我想事先做好业务逻辑。
有什么问题吗?
错误发生在哪里? – SLaks
你确定你正在为你的模型分配一个'IEnumerable'吗?另外,你确定枚举的每个元素都不为null吗?如果你有null元素,'item.article'可能是源错误。 –
Tejs
嗨,当我在@ Html.DisplayFor(modelItem => item.article1)在foreach中,我有字符串结果,这意味着不是空的没有? – dtjmsy