2
@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>
}

上記のコードは、いくつかの問題を引き起こしています。ShortImage をプルしていることに気付くでしょう。これは問題ありません。ただし、プルするイメージがない場合は、エラーが発生します。これを考慮してコードを修正するにはどうすればよいですか?

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 
4

2 に答える 2