簡體   English   中英

在MVC4中使用@ Html.RenderAction無法顯示頁面

[英]Page not displaying using @Html.RenderAction in MVC4

我的問題是,當我在布局中使用@ Html.RenderAction或@ Html.Action並從Controller頁面調用Action方法時,根本沒有顯示。 但是當我在布局中使用靜態屬性時,一切正常。

布局:

  <li>
   <a href="#"><i class="fa fa-fw"></i> Category<span class="fa arrow">  </span></a>
           <ul class="nav nav-second-level">
                   @{
                     Html.RenderAction("Category","Home");
                    }
           </ul>
   </li>

動作方法:

public ActionResult Category()
    {
        int memberId = WebSecurity.GetUserId(User.Identity.Name);

        return PartialView("_Categories", _db.Query<Category>().ToList().Where(u => u.UserId == memberId));
    }

PartialView:

@model IEnumerable<PasswordCloudApp.Models.Category>

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

@foreach (var item in Model)
{
<li>
    @Html.ActionLink(item.Name, "Index", "Entry", new { id = item.Id }, null)
</li>
}

我嘗試過僅使用常規視圖,然后使用部分視圖,但均無效果。 有什么建議嗎?

只是一個想法,不確定

您應該從您的局部視圖@{ Layout = "~/Views/Shared/_Layout.cshtml"; }刪除此行@{ Layout = "~/Views/Shared/_Layout.cshtml"; } @{ Layout = "~/Views/Shared/_Layout.cshtml"; }然后給一試。 希望這可以幫助

根據我的理解,請返回完整的view路徑,例如~/Views/Shared/_Categories.cshtml"而不是_Categories 。如果要布局,請使用相同的代碼,否則請使用

@{ Layout = null; }

刪除布局並按以下方式使用:

@Html.Action("Category","Home")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM