ASP MVC定义截面局部图 [英] ASP MVC Define Section in Partial View

查看:109
本文介绍了ASP MVC定义截面局部图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题状态,我想定义局部视图的部分。

As the title states, i want to define a section in a partial view.

我的code如下:

控制器:

    public ActionResult Test()
    {
        return View();

    }

    public ActionResult PartialTest()
    {
        return PartialView("_PartialTest");
    }

Test.cshtml:

Test.cshtml:

@{
ViewBag.Title = "Test";
}

<h2>Test</h2>


@Html.Action("PartialTest")

_PartialTest.cshtml:

_PartialTest.cshtml:

<p>partial Test</p>

@section scripts{
    <script type="text/javascript">
        $(document).ready(function() {
            alert("Test");
         });

     </script>
}

在Test.cshtml配售部分脚本工作正常,所以问题并不在布局。

Placing the section "scripts" in the Test.cshtml works fine so the problem isn't in the layout.

任何人都知道如何做到这一点?

Anyone know how to do this?

推荐答案

部分视图不支持 @section 标记。你应该参考的是局部视图的视图添加它们。更多信息请参见这个问题:<一href=\"http://stackoverflow.com/questions/7556400/injecting-content-into-specific-sections-from-a-partial-view-asp-net-mvc-3-with\">Injecting内容转换成特定部分从局部视图ASP.NET MVC 3的Razor视图引擎。

Partial views don't support @section tag. You should add them in the view which references the partial view. See this question for more information: Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine.

有基本上归结为一个事实,即引用的局部主视图应负责包括JavaScript,本身不是局部视图。

It basically comes down to the fact that the main view referencing a partial should be responsible for including Javascript, not the partial view itself.

这篇关于ASP MVC定义截面局部图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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