none
MVC中使用ViewBag传递数据报错 值不能为 null。 参数名: virtualPath RRS feed

  • 问题

  • 值不能为 null。
    参数名: virtualPath
    
    说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
    
    异常详细信息: System.ArgumentNullException: 值不能为 null。
    参数名: virtualPath
    
    源错误: 
    
    
    行 5:  @{
    行 6:      var partinfos = ViewBag.partinfo != null ? (List<original_part_info>)ViewBag.partinfo : null;
    行 7:      var subjectlist = ViewBag.subjectlist as List<SelectListItem>;
    行 8:  }
    行 9:  
    
    源文件: e:\SVN\Solution_Comic_v1.0\Admin_v2\Views\xxxxxx\BookInfo.cshtml    行: 7 
    
    堆栈跟踪: 
    
    
    [ArgumentNullException: 值不能为 null。
    参数名: virtualPath]
       System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options) +9926013
       System.Web.VirtualPathUtility.Combine(String basePath, String relativePath) +30
       System.Web.WebPages.UrlUtil.Url(String basePath, String path, Object[] pathParts) +17
       System.Web.WebPages.WebPageExecutingBase.Href(String path, Object[] pathParts) +25
       ASP._Page_Views_Original_BookInfo_cshtml.Execute() in e:\SVN\Solution_Comic_v1.0\Admin_v2\Views\Original\BookInfo.cshtml:7
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +196
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +127
       System.Web.WebPages.StartPage.RunPage() +19
       System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
       System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +259
       System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +294
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +15
       System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242
       System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +25
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242
       System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +25
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +175
       System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89
       System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +58
       System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +44
       System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +15
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +16
       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +58
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +54
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +16
       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +58
       System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +44
       System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +12
       System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +16
       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +58
       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +11
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9644037
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

    @model EFDal.Models.original_book_info
    @using EFDal.Models;
    @using EFDal.Models.Enums;
    @{
        var partinfos = ViewBag.partinfo != null ? (List<original_part_info>)ViewBag.partinfo : null;
        var subjectlist = ViewBag.subjectlist as List<SelectListItem>;
    }
    
    
    
      public ActionResult BookInfo(int bookid)
            {
                using (xxxContext db = new xxxContext(ContextType.xxxlRead))
                {
                    var bookinfo = db.aa.Find(bookid);
                    var partinfo = db.cc.Where(x => !x.isdelete && !x.ispause && x.bookid == bookid).ToList();
                    if (partinfo.Count > 0)
                    {
                        ViewBag.partinfo = partinfo;
                    }
                    var subjectlist = vv.BLL.CommonHelper.ss(false);
                    ViewBag.subjectlist = subjectlist;
                    return View(bookinfo);
                }
            }

    大神求助呀  找了好久没找到是什么原因哎.....


    2016年1月4日 6:33

答案

  • 你好,

    我觉得问题可能在于subjectlist,你从数据库返回的是Null。你可以设置一个断点查看一下。

    另外,我建议你参考下面的代码:

    Controller中的代码:

                List<SelectListItem> list = new List<SelectListItem>();
                list = context.Employees.Select(c => new SelectListItem() { Text = c.City, Value = c.Country }).ToList();
                ViewBag.CountryList = list;

    View中的代码:

           @Html.DropDownList("ddl", (IEnumerable<SelectListItem>)ViewBag.CountryList)

    2016年1月5日 2:28
    版主