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

最新文章

  1. thinkphp-二次开发1
  2. jquery 获取多个select name 的值
  3. Xposed Module开发教程1
  4. HoloLens开发手记 - Unity之Recommended settings 推荐设置
  5. ios cocos2d 画线出现闪烁问题
  6. 设置ajax 同步执行
  7. VxWorks 6.9 内核编程指导之读书笔记 -- POSIX
  8. c/c++面试总结(1)
  9. Linux自动备份MySQL数据库脚本代码
  10. Angularjs在线api文档
  11. .Net小白的第一篇博客
  12. Jmeter+Ant+Jenkins接口自动化测试(一)_环境部署
  13. java 命令行JDBC连接Mysql
  14. 查看容器IP地址
  15. Spark项目之电商用户行为分析大数据平台之(六)用户访问session分析模块介绍
  16. 中国移动DNS IP地址大全(32个省)
  17. JAVA I/O(一)基本字节和字符IO流
  18. Python学习之路day3-集合
  19. debian 7 linux 安装jdk出现Error occurred during initialization of VM java/lang/NoClassDefFoun
  20. 【Leetcode】【Medium】Simplify Path

热门文章

  1. Android 的学习心得
  2. 【OCP-12c】CUUG 071题库考试原题及答案解析(20)
  3. leecode刷题(16)-- 字符串转换整数
  4. BZOJ 1412--狼和羊的故事(最小割)
  5. java使用memcached2--集群部署
  6. JAVA构造函数(方法)
  7. 如何检查 IP是否冲突了
  8. mxonline实战6 , 忘记用户密码时进行重置
  9. axios跨域问题
  10. 人工鱼群算法超详细解析附带JAVA代码