本文转自: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 } })

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

  1. unity can't add the script behaviour CallbackExecutor. The script needs ...

    记一次Unity3D小问题 当我打开VS2017编辑完C#脚本,要将脚本拖到一个3D组件上时,发生了以下错误 unity can't add the script behaviour Callback ...

  2. arcgis python arcpy add data script添加数据脚本

    arcgis python arcpy add data script添加数据脚本mxd = arcpy.mapping.MapDocument("CURRENT")... df ...

  3. 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 Departme ...

  4. Unity3d 拖拽脚本报错Can't add the script component "" because the script class cannot be found

    解决办法: ①报错原因:文件名与文件内容中的类名不相符. ②关闭360.鲁大师等防护软件,重新安装系统.

  5. 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. ...

  6. Add a Preview to a List View将预览添加到列表视图

    In this lesson, you will learn how to show a preview section in a List View's grid. For this purpose ...

  7. Using Script and Style Bundles【翻译】

    遇到个MVC4中的bundles的问题,问了别人,由于不熟悉MVC4,始终问不到点子上,所以自己就翻译了下资料,搞明白了这个VS显示正常IIS显示异常的小问题,申明我翻译的很烂,不过共享出来或许会帮到 ...

  8. add .json handler support in IIS 7

    Sometimes we need to create JSON in a text file with extension .json, however by default IIS 7 or an ...

  9. web前端-----JAVA Script(一)

      JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase ...

随机推荐

  1. c# 图片等比缩略

    public Bitmap getnew(Image bit, int TargetWidth, int TargetHeight)//beishu参数为放大的倍数.放大缩小都可以,0.8即为缩小至原 ...

  2. Asp.net Core过滤器

    Asp.net Core五类过滤器:Authorization Filter.Resource Filter.Acton Filter.Exception Filter.Result Filter.优 ...

  3. 【总结】 BZOJ前100题总结

    前言 最近发现自己trl,所以要多做题目但是Tham布置的题目一道都不会,只能来写BZOJ HA(蛤)OI 1041 复数可以分解成两个点,所以直接把\(R^2\)质因数分解一下就可以了,注意计算每一 ...

  4. C博客作业05—指针

    1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1 学习体会 理解了指针在代码中的使用方法,学会使用指针进行参数操作 学会了结构体的定义方式与结构体的使用 经过持续一个周 ...

  5. ceph luminous 新功能之内置 dashboard

    # 开启 dashboard (在任一 mon_server 节点上)ceph mgr module enable dashboard # 设置dashboard 端口和IPceph config-k ...

  6. Exp1 PC平台逆向破解 20164323段钊阳

    实验目标 学习两种方法运行代码片段,并学习如何注入运行任何Shellcode. 三个实验内容如下: 1.手工修改可执行文件,改变程序执行流程,直接跳转到getshell函数 2.利用foo函数的bof ...

  7. GO语言官方中文教程!

    官方中文教程网址:https://tour.go-zh.org/basics/1 推荐理由:简洁,一句废话没有,对于初学者可以让大家快速掌握GO语言! 注意问题:如果不能访问,你懂的! 教程截图:

  8. CTF web题型解题技巧

    工具集 基础工具:Burpsuite,python,firefox(hackbar,foxyproxy,user-agent,swither等) 扫描工具:nmap,nessus,openvas sq ...

  9. loadrunner录制的基本知识

    1.http/html录制选择web_url,如下图所示: 开始录制->Options->Recording->HTML Advanced->选择web_url->OK ...

  10. CodeChef March Challenge 2019题解

    传送门 \(CHNUM\) 显然正数一组,负数一组 for(int T=read();T;--T){ n=read(),c=d=0; fp(i,1,n)x=read(),x>0?++c:++d; ...