I'm getting a ArgumentNullException and i dont know how to fix this. Ive tried DefaultIfEmpty but it still doesnt work.
Here is the code:
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)
}
}
And here is the error:
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)
Here is the code of my DefauftActionForControllers:
using System.Collections.Generic;
namespace Heijligers.Mas.Web.Models
{
public class DefaultActionForControllers
{
public string Controller { get; set; }
public string Action { get; set; }
}
}
i changed the code to this, it didn't work tho:
var defaultActionForController = ((List<DefaultActionForControllers>)ViewContext.Controller.ViewBag.DefaultActionForControllers).FirstOrDefault(s => s.Controller == controller).Action;
it still gave me a ArgumentNullException. When i went and debug, it said var defaultActionForController = null And in ViewBag.DefaultActionsForControllers count 12
idk if that information helps in any way, but i thought of just posting it u never know
The error stack shows you what the issue is:
Whereis an extension method. As such, it operates on a source and the error is telling you thesourceparameter isnull.Check the code that populates
ViewBag.DefaultActionsForControllers. You need to set the value on the controller's action prior to rendering the view.Lastly, your method call can be simplified as
FirstOrDefaultallows a predicate as well: