[转]How to add a script in a partial view in MVC4?

本文转自:https://stackoverflow.com/questions/14114084/how-to-add-a-script-in-a-partial-view-in-mvc4

问题:

This is the code which I have in my partial view

@model Contoso.MvcApplication.Models.Exercises.AbsoluteArithmetic

@using(Html.BeginForm())
{
<div>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">@Model.Number1</span>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">+</span>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">@Model.Number2</span>
    <span style="width: 110px; float:left; text-align:center; font-size:2.5em;">=</span>
    <span>
            @Html.EditorFor(model => model.Result)
            @Html.ValidationMessageFor(model => model.Result)
    </span>
</div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Please note at the bottom of my code, I‘ve got a @section, and I realized that it‘s not running if I set a breakpoint there. If I move that line in the _Layout.cshtml it works well, but that‘s not the idea.

How can I tell to MVC4 in a partial razor view that I want to add that library?

参考答案:

This worked for me allowing me to colocate JavaScript and HTML for partial view in same file for ease of readability

In View which uses Partial View called "_MyPartialView.cshtml"

<div>
    @Html.Partial("_MyPartialView",< model for partial view>,
            new ViewDataDictionary { { "Region", "HTMLSection" } } })
</div>

@section scripts{

    @Html.Partial("_MyPartialView",<model for partial view>,
                  new ViewDataDictionary { { "Region", "ScriptSection" } })

 }

In Partial View file

@model SomeType

@{
    var region = ViewData["Region"] as string;
}

@if (region == "HTMLSection")
{

}

@if (region == "ScriptSection")
{
        <script type="text/javascript">
    </script">
}

另外关于 aspnetcore下的 

How to use ViewDataDictionary with Html.Partial in asp.net core?

Another way to use this is to pass the ViewData of the current view into the constructor. That way the new ViewDataDictionary gets extended with the items you put in using the collection initializer.

@Html.Partial("MyPartial", new ViewDataDictionary(ViewData) { { "BookId", Model.Id } })
时间: 2024-08-08 09:18:01

[转]How to add a script in a partial view in MVC4?的相关文章

Add an Editor to a Detail View 将编辑器添加到详细信息视图

In this lesson, you will learn how to add an editor to a Detail View. For this purpose, the Department.Office property will be added to the Contact Detail View. You will also learn how to change the layout of a Detail View's editors. 在本课中,您将学习如何将编辑器添

Fragment(10)FragmentTransaction.add(id,Fragment)报错: No view found for id 0x****** for fragment

1.错误信息: 06-26 22:54:28.509: E/AndroidRuntime(20363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exe.custommenu/com.exe.custommenu.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0xaabbcc for fragmen

How to add taxonomy element to a summary view?

[re: Orchard CMS] This caused me scratching my head for days and now I can even feel it's bleeding. The answer however is the simplest. Because it's not obviously indicated anywhere, I was anticipating the layout only works for the detail view of A c

Python自动化开发从浅入深-进阶(script,dom,jquery 基础)

JavaScript (可以对html标签及CSS进行操作) Dom (API,提供找寻html中的标签,以便于使用JS对所找到的标签或CSS进行相应的操作) jQuery (用于将复杂的逻辑和运算封装到一个包中供外部调用) JavaScript: <srcipt>...</script> 存在的形式:文件,块 一般在body的底部书写. --- 声明变量: name = "alex" :(全局变量) var age=18:     (局部变量) --- 数字:

jquery和vue对比

jquery和vue对比 前言:很多人说jquey和vue没有什么可比的,应该和Angular,React来比吧,我到觉得他们倒没有多大的可比性,都是基于mvvm思想设计的框架,无非就是实现的方式不一样,在不同场景下性能上会有一些差异.然而从jquery到vue或者说是到mvvm的转变则是一个思想想的转变,是将原有的直接操作dom的思想转变到操作数据上去,难道不是一个根本性的改变吗? 1.jquery介绍:想必大家都用过jquery吧,这个曾经也是现在依然最流行的web前端js库,可是现在无论是

在iOS中创建静态库

如果你作为iOS开发者已经有一段时间,可能会有一套属于自己的类和工具函数,它们在你的大多数项目中被重用. 重用代码的最简单方法是简单的 拷贝/粘贴 源文件.然而,这种方法很快就会成为维护时的噩梦.因为每个app都有自己的一份代码副本,你很难在修复bug或者升级时保证所有副本的同步. 这就是静态库要拯救你的.一个静态库是若干个类,函数,定义和资源的包装,你可以将其打包并很容易的在项目之间共享. 在本教程中,你将用两种方法亲手创建你自己的通用静态库. 为了获得最佳效果,你应该熟悉Objective-

表单提交+异步请求

@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <script src="~/Scripts/jquery-1.8.2.js"></script> <title>Add</title> <

CRUD Operations In ASP.NET MVC 5 Using ADO.NET

Background After awesome response of an published by me in the year 2013: Insert, Update, Delete In GridView Using ASP.Net C#. It now has more than 140 K views, therefore to help beginners I decided to rewrite the article i with stepbystep approach u

开发技巧汇总|对于imag.js你不知道的事

1.imag.js里有哪些标准JavaScript对象? imag.js里的标准JavaScript对象有Object, Function, Array, Boolean, Date, Math, Number, String, RegExp,Global Functions, JSON. 2.为什么客户端会提示XML语法错误? imag.js的代码文档遵循严格的XML语法规范,开发时要注意以下地方: 1. label, script, web等标签的text可能含有XML特殊符号<.&,