@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/[email protected](item.ID.ToString())">@Html.Raw(item.Title)</a></h2>
<p class="greytxt">@Html.Raw(item.Description.Left(125))<a href="/resources/[email protected](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
参考方案
这是一个真正的技巧,因为您永远不要在View中放入这样的逻辑,但是您可以这样做;
@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/[email protected](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/[email protected](item.ID.ToString())"> more</a>
}
else
{
@Html.Raw(item.Description)<a href="/resources/[email protected](item.ID.ToString())"> more</a>
}
</p>
</div>
</li>
}
真正应该做的是,其中的“模型”部分不应允许您接收视图不可接受的数据。
将asp.net.core 2.0应用发布到IIS时JavaScript无法运行 - javascript我是这里的新手。我有一个VS2017 asp.net C#Web应用程序。我目前不在使用Angular。这是一个简单的网站。我隐藏了div,当用户单击菜单项时,相关的div出现,而我隐藏了其余的div。我使用JavaScript完成此任务。在开发和IIS Express中,它可以工作。当我发布到IIS时,它不起作用。静态页面显示“确定”,但菜单按钮不执行任何…
使用Java脚本基于另一个控件项来验证ASP.NET C#控件 - c#我正在尝试确保一个下拉框或另一个下拉框具有选定的条目,不能两者都选,并且不能都留空/未选择。每次单击运行javascript验证代码的搜索按钮时,即使我只是从一个下拉列表中选择,甚至从两个下拉列表中都没有选择,我都会收到一条消息,好像在两个下拉框中都选择了一样!这可能是我的逻辑,但是,我认为也可能是我的变量读为null。是否有人对问题可能是什么以及如何解决这…
Div单击与单选按钮相同吗? - php有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date…
故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…
ASP.net C#崩溃一行 - c#我有一个母版页,在on load事件中包含以下几行: string menuIDdata = Page.Request.QueryString["mid"]; menuID = 0; // Get the menu ID if (!int.TryParse(menuIDdata, out menuID)) { menuID = 0; } …