Html Helper未在ASP.Net mvc3中检测到该模型



在我当前的项目中,我必须在ASP.Net MVC3 Razor中实现从数据库动态加载树菜单。下面的结构显示了我的菜单模型。MVC3专业水平:入门

MenuId名称ParentMenuId OrderBy

1项目1空1 2项目2空2 3项目2.1
2 1 4项目2.1.1 3 1 5项目2.1.2 3 2

为此,我做了以下事情1.创建的Htmlhelper包含用于菜单和子菜单的静态类,2.为菜单创建模型,3.将HtmlHelper命名空间添加到views/web.config

当我们调试代码时,它会显示一个值不能为null的错误。但我的数据库包含数据,即计数=11错误是由于菜单计数为零。那么如何解决这个问题,因为菜单表包含11行数据

错误

值不能为null。参数名称:来源

描述:在执行期间发生未处理的异常当前web请求。请查看堆栈跟踪以了解更多信息有关错误的信息以及错误在代码中的来源。

异常详细信息:System.ArgumentNullException:值不能为null。参数名称:来源

来源错误:

第14行:{第15行:字符串htmloutput=字符串.Empty;第16行:if(menu.Count()>0)第17行:{第18行:htmloutput+=";

源文件:D:\VSProjects\Elixir\Elixir\HtmlHelpers\HtmlHelperExtension.cs行:16

堆栈跟踪:

[ArgumentNullException:值不能为null。参数名称:源]D:\VS中的System.Linq.Enumerable.Count(IEnumerable1 source) +4164811
Elixir.HtmlHelpers.HtmlHelperExtensions.ParentMenus(HtmlHelper html, IEnumerable
1菜单)Projects\Elixir\Elixir\HtmlHelpers\HtmlHelperExtensions.cs:16
d:\VS中的ASP_Page_Views_Home_Menus_shtml.Execute()Projects\Elixir\Elixir\Views\Home\Menus.cshtml:4
System.Web.WebPages.WebPageBase.ExecutePageHierarchypageContext,TextWriter编写器,WebPageRenderingBase startPage)+88
System.Web.Mvc.RazorView.RenderView(ViewContext ViewContext,TextWriter编写器,对象实例)+220
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContextviewContext,TextWriter编写器)+115
System.Web.Mvc.HtmlHelper.RenderPartialInternal(字符串partialViewName,ViewDataDictionary视图数据,对象模型,TextWriterwriter,ViewEngineCollection ViewEngineCollection)+279
System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper HtmlHelper,字符串部分ViewName,对象模型,ViewDataDictionary视图数据)+113 System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper HtmlHelper,String partialViewName)+31
ASP_d:\VS中的Page_Views_Home_Index_shtml.b2()Projects\Elixir\Elixir\Views\Home\Index.cshtml:16
System.Web.WebPages.<>c
_DisplayClassb.b_9(TextWritertw)+283 System.Web.WebPages.HelperResult.WriteTo(TextWriterwriter)+12 System.Web.WebPages.WebPageBase.Write(HelperResult结果)+67 ASP_中的Page_Views_Shared_LayoutUser_cshtml.Execute()d: \VS Projects\Elixir\Elixir\Views\Shared_LayoutUser.cshtml:136
System.Web.WebPages.WebPageBase.ExecutePageHierarchypageContext,TextWriter编写器,WebPageRenderingBase startPage)+88
System.Web.WebPages。<>c_DisplayClass7.b_6(TextWriterwriter)+102 System.Web.WebPages.HelperResult.WriteTo(TextWriterwriter)+12 System.Web.WebPages.WebPageBase.Write(HelperResultresult)+67
System.Web.WebPages.WebPageBase.RenderSurrounding(字符串partialViewName,Action1 body) +66
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
1 continuation)+260
System.Web.Mvc.<>c_显示类1e.b_1b()+19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContextcontrollerContext,IList1 filters, ActionResult actionResult) +177
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
1.b__7(IAsyncResult)+12 System.Web.Mvc.Anc.WappedAsyncResult`1.End()+62 System.Web.Mvc。<>c_DisplayClasse.b_d()+50
System.Web.Mvc.SecurityUtil.b
_0(操作f)+7System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(操作操作)+22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+60
System.Web.Mvc MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAyncResultresult)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+8862381 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,Boolean和completedSynchronously)+184

HTML帮助程序代码

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;
}
}
}

控制器代码

public ActionResult Menus()
{
var MMenu = ment.Menu.ToList();
return PartialView(MMenu);
}

菜单.cshtml(局部布局)

@model IEnumerable<Elixir.Models.Menus>
<div>
@Html.Raw(@Html.ParentMenus(Model))

</div>

索引页

@{
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>

菜单型号

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; }
}
}

错误

您的Menu.cshtml应该是Menu.cshtml吗?

如果是这样的话,你在这条线上称它为分部:

@Html.Partial("Menus");

但你并没有给它一个模型。

尝试使用

@Html.Action("Menus")

最新更新