2013-02-11 162 views
9

我有一个ASP.NET MVC 4值不能为空或空白。参数名:的contentPath

多层次布局母版页我有以下几点:

<title>@ViewBag.Title</title> 

布局页的顺序如下:

  1. _Layout.cshtml
  2. _SubLayout.cshtml(基于_Layout.cshtml)
  3. Index.cshtml(基于_SubLayout.cshtml)

我在Index Action中设置了@ ViewBag.Title。但是,我得到以下例外:

值不能为空或为空。参数名:的contentPath


这里是我的代码。我只是做的ASP.NET设计模式书的代码,VS 2012/MVC 4

_Layout.cshtml

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>@ViewBag.Title</title> 
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 
    <meta name="viewport" content="width=device-width" /> 
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
<div id="main">  
    <div id="header">   
     <span><a href="@Url.Content("")"> 
     <img alt="Agatha's Clothing Store" 
      src="@Url.Content("/Content/Images/Structure/lg_logo.png")" 
      border="0" /></a></span>   
    </div> 

    <div id="headerSummary">   
     @RenderSection("headerBasketSummary", required: false)     
    </div> 
    <div class="topBarContainer">   
     <div id="background">   
      <div id="navigation"> 
       @RenderSection("MenuContent", required: false) 
      </div>  
      <div id="content"> 
       @RenderBody() 
      </div>    
      <div style="clear: both;" /> 
     </div> 
    </div>   
    @Html.Partial("_SiteFooter") 
</div> 

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script> 
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script> 
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-jtemplates.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Scripts/agatha-common-scripts.js")"></script> 

</body> 
</html> 

_ProductCatalogProduct.cshtml

@model BaseProductCatalogPageView 
@using Agathas.Storefront.Controllers.ViewModels.ProductCatalog 
@using Agathas.Storefront.UI.Web.MVC.Helpers 

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml";     
} 

@if (IsSectionDefined("MenuContent")) 
{ 
    @RenderSection("MenuContent", required: false) 
} 
else 
{ 
    @Html.Partial("_Categories", ((BaseProductCatalogPageView)Model).Categories) 
} 
@RenderBody(); 

指数工作.cshtml

@model HomePageView 
@using Agathas.Storefront.Controllers.ViewModels.ProductCatalog 
@using Agathas.Storefront.Services.ViewModels 
@using Agathas.Storefront.UI.Web.MVC.Helpers 

@{ 
    Layout = "~/Views/Shared/_ProductCatalogLayout.cshtml"; 
    ViewBag.Title = "Home Page"; 
} 

<img width="559" height="297" 
     src="@Url.Content("~/Content/Images/Products/product-lifestyle.jpg")" 
     style="border-width: 0px; padding: 0px; margin: 0px" /> 
    <div style="clear: both;"></div> 
    <h2>Top Products</h2> 
    <div id="items" style="border-width: 1px; padding: 0px; margin: 0px"> 
     <ul class="items-list"> 
      @foreach (ProductSummaryView product in Model.Products) 
      { 
       <li class="item-detail"> 
       <a class="item-productimage-link" href="@Url.Action("Detail", "Product",  new { id = product.Id }, null)"> 
      <img class="item-productimage" src="@Url.Content("~/Content/Images/Products/" + product.Id.ToString() +".jpg"))" /></a> 
      <div class="item-productname">@Html.ActionLink(product.BrandName + " " + product.Name, "Detail", "Product", new { id = product.Id }, null)</div> 
      <div class="item-price">@product.Price</div> 
      </li> 
     } 
    </ul> 
</div> 

非常感谢

+0

你能提供完整的堆栈跟踪吗? – nemesv 2013-02-11 15:52:19

+0

愚蠢的问题,但两个ViewBag被称为相同的,相同的大小写......你能提供一些代码,以更好地了解问题? – 2013-02-11 16:41:37

回答

0

这可以,如果你要分配在也在父一个集儿童Razor视图文件中的值发生,但会显示在部分由Layout.cshtml

名为示例

_Layout.cshtml | (的父母) | Parent.cshtml | (的父母) | Page1.cshtml

在Page1.cshtml我做

ViewBag.Title= "Value1"; 

在Parent.cshtml我做相同的,但使用一把umbraco电话:

ViewBag.Title = Umbraco.Field("value").ToString(); 

解决方法是去除分配Parent.cshtml。这似乎是一个错误,我担心这是我找到的唯一修复程序。希望它可以帮助你...

20

这段代码抛出异常:<a href="@Url.Content("")">。发生这种情况是因为Url.Content方法的contentPath参数不能为空或为空。

相关问题