错误帮助:超出界限

时间:2013-04-02 18:32:57

标签: c# asp.net asp.net-mvc asp.net-mvc-3

@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 

2 个答案:

答案 0 :(得分:4)

这是一个真正的黑客,因为你永远不应该在你的视图中放置这样的逻辑,但你可以这样做;

@foreach (var item in Model)
{
    <li>
        <div class="lnewsblock">

            <p class="newsthumb">
            @if(item.ShortImage != null)
            {
                <img src="@Html.Raw(item.ShortImage)" width="90" height="71" alt="" /></p>
            }
            </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">
            @if(item.Description.Length >= 125)
            {
                @Html.Raw(item.Description.Left(125))<a href="/resources/newsdetail?id=@Html.Raw(item.ID.ToString())"> more</a>
            }
            else
            {
                @Html.Raw(item.Description)<a href="/resources/newsdetail?id=@Html.Raw(item.ID.ToString())"> more</a>
            }
            </p>
        </div>
    </li>
}

真正应该做的是,“模型”部分应该不允许您接收视图无法接受的数据。

答案 1 :(得分:2)

不幸的是,你的堆栈跟踪没有多大帮助(至少,不是我)。开发的系统在程序的这个特定部分有很多进展,没有看到它的代码我不能给你一个具体的答案或建议这个问题。话虽这么说,我想与您分享一个替代方法来解决您的问题。

前段时间我需要找到一种有效的方法来渲染带有锚标签的图像,以作为图像链接或按钮。虽然在硬编码的HTML中执行此操作是可行的...图像通常是动态的,因此我通过使用一些片段扩展MVC框架找到了更具编程性的方法。这是:

public static MvcHtmlString ActionImage(this HtmlHelper html, string action, object routeValues, string imagePath, string alt)
    {
        var url = new UrlHelper(html.ViewContext.RequestContext);

        // build the <img> tag
        var imgBuilder = new TagBuilder("img");
        imgBuilder.MergeAttribute("src", url.Content(imagePath));
        imgBuilder.MergeAttribute("alt", alt);
        string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

        // build the <a> tag
        var anchorBuilder = new TagBuilder("a");
        anchorBuilder.MergeAttribute("href", url.Action(action, routeValues));
        anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
        string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

        return MvcHtmlString.Create(anchorHtml);
    }

这不是最强大的扩展,但它符合我的目的,它也可以为你服务。将变量写入HTML属性(例如:src for images)是危险的,因为任何具有基本HTML知识的人都可能通过一个小的调整来破坏他们进入后端的方式。这是我对 jadarnel27 解决方案的主要关注,因为它将变量保存在我不喜欢的地方。

这个简单的扩展程序允许您在视图中动态,安全地创建链接图像,只需一个简单的单行程序:

<p class="site-title">@Html.ActionImage("Index", null, "~/Images/logo_sm.jpg", "Index")</p>

如果你去适应这个扩展你的程序需要,你可以做所有的错误处理的后端相当类似的 jadarnel27 的解决方案。这既可以解决您的原始问题,又可以使应用程序更安全。

不幸的是,你所提到的,这是不是你的专业领域,并通过它的外观,你需要做出一些漂亮的大规模扫到应用程序的变化,如果它是所有编码像你张贴的所有片段以上。无论如何,我希望这至少可以帮助您和您的公司做出明智的决定。