Reputation: 426
In my current project i have to implement Dynamically loading tree menu from database in ASP.Net MVC3 Razor.The below structure shows my Menu Model. Level of Expertise in MVC3: Beginner
MenuId Name ParentMenuId OrderBy
1 Item1 Null 1 2 Item2 Null 2 3 Item 2.1
2 1 4 Item 2.1.1 3 1 5 Item 2.1.2 3 2
for that i have done the following things 1.Created Htmlhelper Contains static classes for Menus and sub menus, 2.Created Model for Menu, 3.added HtmlHelper namespace to views/web.config
When we debug the code it shows an error that value cannot be null.But my db contains the data ie count =11 The error is due to menu counting to Zero.So how to solve this because menu table contains 11 rows of data?
Error
Value cannot be null. Parameter name: source
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.ArgumentNullException: Value cannot be null. Parameter name: source
Source Error:
Line 14: { Line 15: string htmloutput=string.Empty; Line 16: if (menu.Count() > 0) Line 17: { Line 18: htmloutput += "";
Source File: D:\VS Projects\Elixir\Elixir\HtmlHelpers\HtmlHelperExtensions.cs Line: 16
Stack Trace:
[ArgumentNullException: Value cannot be null. Parameter name: source] System.Linq.Enumerable.Count(IEnumerable
1 source) +4164811
1 menu) in D:\VS Projects\Elixir\Elixir\HtmlHelpers\HtmlHelperExtensions.cs:16
Elixir.HtmlHelpers.HtmlHelperExtensions.ParentMenus(HtmlHelper html, IEnumerable
ASP._Page_Views_Home_Menus_cshtml.Execute() in d:\VS Projects\Elixir\Elixir\Views\Home\Menus.cshtml:4
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +279
System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +113 System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName) +31
ASP._Page_Views_Home_Index_cshtml.b_2() in d:\VS Projects\Elixir\Elixir\Views\Home\Index.cshtml:16
System.Web.WebPages.<>c_DisplayClassb.b__9(TextWriter tw) +283 System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +12 System.Web.WebPages.WebPageBase.Write(HelperResult result) +67 ASP._Page_Views_Shared__LayoutUser_cshtml.Execute() in d:\VS Projects\Elixir\Elixir\Views\Shared_LayoutUser.cshtml:136
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88
System.Web.WebPages.<>c_DisplayClass7.b_6(TextWriter writer) +102 System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +12 System.Web.WebPages.WebPageBase.Write(HelperResult result) +67
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action1 body) +66
1 continuation) +260
System.Web.WebPages.WebPageBase.PopContext() +262
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func
System.Web.Mvc.<>c_DisplayClass1e.b_1b() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +177
1.b__7(IAsyncResult ) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.<>c_DisplayClasse.b_d() +50
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8
System.Web.Mvc.SecurityUtil.b_0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
HTML HELPER CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Elixir.Models;
namespace Elixir.HtmlHelpers
{
public static class HtmlHelperExtensions
{
public static string ParentMenus(this HtmlHelper html,IEnumerable<Menus> menu)
{
string htmloutput=string.Empty;
if (menu.Count() > 0)
{
htmloutput += "<ul class='side-navigation accordion' id='nav-accordion'>";
var MainMenu = from mainMenu in menu where mainMenu.MainMenuId == null orderby mainMenu.MenuOrder select mainMenu;
foreach(Menus m in MainMenu)
{
htmloutput += "<li>";
htmloutput += "<li><i class='icon-home'>";
htmloutput += LinkExtensions.ActionLink(html, m.LinkName, m.ActionName, m.ControllerName);
htmloutput += "</li>";
htmloutput += "</li></i>";
}
htmloutput += "</ul>";
}
return htmloutput;
}
public static string SubMenu(this HtmlHelper html, IEnumerable<Menus> SubMenu, int MenuId)
{
string htmlOutput = string.Empty;
if (SubMenu.Count() > 0)
{
htmlOutput += "<ul class='side-navigation accordion' id='nav-accordion'>";
var subMenu = from SMenu in SubMenu where SMenu.MainMenuId == MenuId orderby SMenu.MenuOrder select SMenu;
foreach (Menus m in subMenu)
{
htmlOutput += "<li>";
htmlOutput += "<li><i class='icon-home'>";
htmlOutput += LinkExtensions.ActionLink(html, m.LinkName, m.ActionName, m.ControllerName);
htmlOutput += "</li>";
htmlOutput += "</li></i>";
}
htmlOutput += "</ul>";
}
return htmlOutput;
}
}
}
Controller Code
public ActionResult Menus()
{
var MMenu = ment.Menu.ToList();
return PartialView(MMenu);
}
Menu.cshtml ( Partial Layout)
@model IEnumerable<Elixir.Models.Menus>
<div>
@Html.Raw(@Html.ParentMenus(Model))
</div>
Index Page
@{
ViewBag.Title = "Elixir ERP V1.0 Beta";
Layout = "~/Views/Shared/_LayoutUser.cshtml";
}
<div class="main-container">
<div class="main-wrapper">
<div class="scroll-top">
<a href="#" class="tip-top" title="Go Top"><i class="icon-arrow-up"></i></a>
</div>
<div class="left-bar merge-left">
<!-- SEARCH BAR -->
<!-- LEFT NAV -->
@section leftnav{
<div class="left-nav">
@Html.Partial("Menus");
</div>
}
</div>
</div>
<div class="container">
</div>
</div>
Menu Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Elixir.Models
{
[Table("tbl_Menus")]
public class Menus
{
[Key]
public int MenuId { get; set; }
public string UserName { get; set; }
public string MenuName { get; set; }
public int MenuOrder { get; set; }
public int? MainMenuId { get; set; }
public string LinkName { get; set; }
public string ActionName { get; set; }
public string ControllerName { get; set; }
}
}
Error
Upvotes: 0
Views: 904
Reputation: 16358
Is your Menu.cshtml supposed to be Menus.cshtml?
If so, you're calling it as a partial on this line:
@Html.Partial("Menus");
But you're not giving it a model.
Try using
@Html.Action("Menus")
Upvotes: 1