提问人:Daniel 提问时间:2/18/2021 最后编辑:VegaDaniel 更新时间:2/23/2021 访问量:520
FirstOrDefault() 我收到 ArgumentNullException
FirstOrDefault() i'm getting a ArgumentNullException
问:
我收到一个 ArgumentNullException,我不知道如何解决这个问题。 我尝试了 DefaultIfEmpty,但它仍然不起作用。
代码如下:
if (controller != "Home")
{
var defaultActionForController = ((List<DefaultActionForControllers>)ViewBag.DefaultActionsForControllers).Where(s => s.Controller == controller).Select(s => s.Action).FirstOrDefault();
if (controller == "ManagementObjects")
{
var controllerName = firstLayerNameSet;
@:> @Html.ActionLink(firstLayerNameSet, defaultActionForController, controller)
}
else if (controller == "MangementCategories")
{
var controllerName = secondLayerNameSet;
@:> @Html.ActionLink(secondLayerNameSet, defaultActionForController, controller)
}
else if (controller == "View")
{
@:> @Html.ActionLink(Resources.ResourceManager.GetString(controller), "", "")
}
else
{
@:> @Html.ActionLink(Resources.ResourceManager.GetString(controller), defaultActionForController, controller)
}
}
这是错误:
3005 An unhandled exception has occurred.
ArgumentNullException
Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
at ASP._Page_Views_Shared__LoginPartial_cshtml.Execute() in d:\inetpub\wwwroot\TestMap\Views\Shared\_LoginPartial.cshtml:line 30
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at ASP._Page_Views_Shared__TopNavbar_cshtml.Execute() in d:\inetpub\wwwroot\TestMap\Views\Shared\_TopNavbar.cshtml:line 13
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at ASP._Page_Views_Shared__Layout_cshtml.Execute() in d:\inetpub\wwwroot\TestMap\Views\Shared\_Layout.cshtml:line 73
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.WebPages.WebPageBase.<>c__DisplayClass40_0.<RenderPageCore>b__0(TextWriter writer)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.HttpApplication.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar)
at System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)
这是我的 DefauftActionForControllers 的代码:
using System.Collections.Generic;
namespace Heijligers.Mas.Web.Models
{
public class DefaultActionForControllers
{
public string Controller { get; set; }
public string Action { get; set; }
}
}
我将代码更改为这个,它不起作用:
var defaultActionForController = ((List<DefaultActionForControllers>)ViewContext.Controller.ViewBag.DefaultActionForControllers).FirstOrDefault(s => s.Controller == controller).Action;
它仍然给了我一个 ArgumentNullException。 当我去调试时,它说var defaultActionForController = null 在 ViewBag.DefaultActionsForControllers 中,计数为 12
IDK如果该信息有任何帮助,但我想过只是发布它,你永远不知道
答:
1赞
JuanR
2/19/2021
#1
错误堆栈显示问题所在:
ArgumentNullException
Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
at ASP._Page_Views_Shared__LoginPartial_cshtml.Execute() in
d:\inetpub\wwwroot\TestMap\Views\Shared\_LoginPartial.cshtml:line 30
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
Where
是一种扩展方法。因此,它对源进行操作,并且错误告诉您参数是 。source
null
检查填充 的代码。在呈现视图之前,您需要设置控制器操作的值。ViewBag.DefaultActionsForControllers
最后,您的方法调用可以简化为允许谓词:FirstOrDefault
var defaultActionForController = ((List<DefaultActionForControllers>)ViewBag.DefaultActionsForControllers).FirstOrDefault(s => s.Controller == controller).Action;
评论
0赞
Daniel
2/22/2021
嗨!,我更改了方法,但它仍然给了我错误。另外,我发布了我的 DefaultActionForControllers 类。也许你能在那里找到问题?
0赞
JuanR
2/22/2021
@Daniel 您未正确设置控制器中的值,或者由于您正在处理动态属性,因此存在 cAsE sEnSiTiViTy 问题。ViewBag.DefaultActionsForControllers
0赞
JuanR
2/22/2021
@Daniel 请发布您的控制器代码。加载此分部视图时正在执行的相关操作的代码。
评论