ASP.NET MVC 3在页面布局管窥 [英] ASP.NET MVC 3 Partial View in layout page

查看:65
本文介绍了ASP.NET MVC 3在页面布局管窥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个asp.net MVC布局页的共享内容(导航)。

I'm working on setting up a shared content (navigation) for an asp.net MVC layout page.

下面是我的局部视图_LayoutPartial.cshtml与code从模型拉动导航数据。

Here is my partial view "_LayoutPartial.cshtml" with code to pull navigation data from a model.

@model MyApp.Models.ViewModel.LayoutViewModel
<p>

    @foreach (var item in Model.navHeader)
    {
        //Test dump of navigation data
        @Html.Encode(item.Name); 
        @Html.Encode(item.URL); 

    }
</p>

下面是code为我的控制器LayoutController.cs的样子。

Here is how the code for my controller "LayoutController.cs" looks like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyApp.Models.ViewModel;

namespace MyApp.Controllers
{
    public class LayoutController : Controller
    {

        //
        // GET: /Layout/

        LayoutViewModel layout = new LayoutViewModel();

        public ActionResult Index()
        {
            return View(layout);
        }

    }
}

下面是code为_Layout.cshtml的页面。我试图在这里所说的局部视图使用Html.RenderAction(动作,控制器)的方法。

Here is the code for the "_Layout.cshtml" page. I'm attempting to call the partial view here using Html.RenderAction(Action,Controller) method.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
    <p>
        @{Html.RenderAction("Index","Layout");}
    </p>

    @RenderBody()
</body>
</html>

在布局页面执行@ {Html.RenderAction(指数,布局);}线,它抛出了为执行处理'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper子请求的错误消息 '。

When the layout page executes the @{Html.RenderAction("Index","Layout");} line, it throws out an error message "Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'."

我缺少什么朋友?我怎样才能调用布局页面的局部视图?

What am I missing friends? How can I call a partial view in a layout page?

感谢大家提前!

推荐答案

而不是:

public ActionResult Index()
{
    return View(layout);
}

做的:

public ActionResult Index()
{
    return PartialView(layout);
}

如果你不这样做,当你从你的孩子的行动恢复正常的观点,这普通视图试图包含布局,这反过来又企图使孩子的行动,这反过来又返回一个视图,从而包括布局,这反过来又企图使孩子动作,...以及我们最终像一个通过这同一个站点移植名称

If you don't do that when you return a normal view from your child action, this normal view attempts to include the Layout, which in turn attempts to render the child action, which in turn returns a view, which in turn includes the Layout, which in turn attempts to render the child action, ... and we end up with names like the one ported by this very same site.

另外,在你的部分,你不需要做双重编码。该@剃刀功能已经做HTML EN code:

Also in your partial you don't need to do double encoding. The @ Razor function already does HTML encode:

@model MyApp.Models.ViewModel.LayoutViewModel
<p>

    @foreach (var item in Model.navHeader)
    {
        @item.Name 
        @item.URL
    }
</p>

这篇关于ASP.NET MVC 3在页面布局管窥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆