简体   繁体   English

我应该使用@html.renderpartial还是@html.renderaction

[英]should I be using an @html.renderpartial or @html.renderaction

I'm trying to bring in my menu. 我正试着带上我的菜单。

In my _Layout.cshtml page I have 在我的_Layout.cshtml页面中

<div class="wrapper">
                <!-- Navigation -->

                  @Html.RenderAction("Navigation", "Nav")

The Nav Controller looks like this 导航控制器看起来像这样

public ActionResult Navigation()
{
    var pages = pageRepository.Pages;
    return View(pages);
}

The Navigation View Looks like this 导航视图看起来像这样

@model IEnumerable<Site.Domain.Entities.Page>
@{
    Layout = null;
    List<Site.Domain.Entities.Page> pages = new List<Site.Domain.Entities.Page>();

    foreach(var page in Model)
    {
        pages.Add(page);
    }
}

@foreach (var link in Model)
{
    if (link.ParentPage == "Home")
    { 
    <li>@link.PageTitle</li>
    <ul>
        @foreach (var subLink in pages)
        {
            if (subLink.ParentPage == link.PageTitle)
            { 
            <li>@subLink.PageTitle</li>
            }
        }
    </ul> 

    }
}

The view works fine when I go to .../nav/navigation 当我去... / nav / navigation时,视图工作正常

What I'm trying to do is bring this into my _Layout page so that I can use it as my menu. 我想要做的是将它带入我的_Layout页面,以便我可以将它用作我的菜单。

I continue to get an error with @Html.RenderAction("Navigation", "Nav") 我继续使用@ Html.RenderAction(“导航”,“导航”)出错

The error says "The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments" 错误说“'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)'的最佳重载方法匹配'有一些无效的参数”

Should I be using this as a partial? 我应该使用它作为部分吗? What is the best way to go about this? 最好的方法是什么? Thanks for any advice! 谢谢你的建议!

For what you're trying to do, @Html.RenderAction(..) is the correct call. 对于你想要做的事情, @Html.RenderAction(..)是正确的调用。 RenderAction is ChildActionExtension and will need to add that attribute to the controller. RenderActionChildActionExtension ,需要将该属性添加到控制器。

Your controller should look something like below. 您的控制器应如下所示。 Note that you will want to return a PartialView as well. 请注意 ,您还需要返回PartialView

[ChildActionOnly]
public ActionResult Navigation()
{
    var pages = pageRepository.Pages;
    return PartialView(pages);
}

The Render action does not return HTML, but rather adds the content to the response. “渲染”操作不返回HTML,而是将内容添加到响应中。 With that, your view should look like: 有了它,您的视图应如下所示:

@{@Html.RenderAction("Navigation", "Nav");}

Reference: http://msdn.microsoft.com/en-us/library/ee721274(v=vs.108).aspx 参考: http//msdn.microsoft.com/en-us/library/ee721274(v = vs.108).aspx

Because Html.RenderAction is a void and does not return a value, you need to "escape" the call with braces 因为Html.RenderAction是一个void而且没有返回值,所以你需要用大括号“转义”调用

@{Html.RenderAction("Navigation", "Nav");}

In your controller, you should return a partial view instead. 在控制器中,您应该返回部分视图。

public ActionResult Navigation()
{
    var pages = pageRepository.Pages;
    return PartialView(pages);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM