@foreach (var item in Model)
{
<li>
<div class="lnewsblock">
<p class="newsthumb">
<img src="@Html.Raw(item.ShortImage)" width="90" height="71" alt="" /></p>
<p class="datetxt">@Html.Raw(item.Date.Value.ToString("ddd MM.dd.yyyy"))</p>
<h2> <a href="/resources/newsdetail?id=@Html.Raw(item.ID.ToString())">@Html.Raw(item.Title)</a></h2>
<p class="greytxt">@Html.Raw(item.Description.Left(125))<a href="/resources/newsdetail?id=@Html.Raw(item.ID.ToString())"> more</a></p>
</div>
</li>
}
上面的代码导致了几个问题。您会注意到它正在拉短图像,这很好。但是,如果没有要 pull 的图像,则会产生错误。我将如何修复代码来解决这个问题?
其次,当它拉取项目描述时,它会尝试拉取第一个125
字符。所以如果描述太短,也会产生错误。 我将如何构建代码来解决这个问题?
Server Error in '/' Application.
Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[IndexOutOfRangeException: Index was outside the bounds of the array.]
SitefinityWebApp.Mvc.Models.NewsModel.RetrieveCollectionOfNews(NewsType ShowType) in D:\SVN\LbyEgglw\LbyEgglw.WebApp\Mvc\Models\NewsModel.cs:53
SitefinityWebApp.Mvc.Controllers.NewsController.Index() in D:\SVN\LbyEgglw\LbyEgglw.WebApp\Mvc\Controllers\NewsController.cs:29
lambda_method(Closure , ControllerBase , Object[] ) +43
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +248
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +125
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +640
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +312
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +691
Telerik.Sitefinity.Mvc.ControllerWrapper.Execute() +133
Telerik.Sitefinity.Mvc.Proxy.MvcControllerProxy.ExecuteController() +2620
System.Web.UI.Control.PreRenderRecursiveInternal() +113
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Control.PreRenderRecursiveInternal() +222
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4201
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272