[转]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 } })
[转]How to add a script in a partial view in MVC4?的更多相关文章
- unity can't add the script behaviour CallbackExecutor. The script needs ...
记一次Unity3D小问题 当我打开VS2017编辑完C#脚本,要将脚本拖到一个3D组件上时,发生了以下错误 unity can't add the script behaviour Callback ...
- arcgis python arcpy add data script添加数据脚本
arcgis python arcpy add data script添加数据脚本mxd = arcpy.mapping.MapDocument("CURRENT")... df ...
- 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 ...
- Unity3d 拖拽脚本报错Can't add the script component "" because the script class cannot be found
解决办法: ①报错原因:文件名与文件内容中的类名不相符. ②关闭360.鲁大师等防护软件,重新安装系统.
- 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. ...
- 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 ...
- Using Script and Style Bundles【翻译】
遇到个MVC4中的bundles的问题,问了别人,由于不熟悉MVC4,始终问不到点子上,所以自己就翻译了下资料,搞明白了这个VS显示正常IIS显示异常的小问题,申明我翻译的很烂,不过共享出来或许会帮到 ...
- 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 ...
- web前端-----JAVA Script(一)
JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase ...
随机推荐
- 关于mysql中[Err] 1451 -Cannot delete or update a parent row: a foreign key constraint fails
mysql> SET FOREIGN_KEY_CHECKS = 0; Query OK, 0 rows affected (0.02 sec) mysql> delete from r ...
- Gogland编译Syncthing!
说明:我仅仅以这个Syncthing工程为例,来说明如何正确使用Goland编译其他人写的工程,应该具有普遍意义,看懂这篇博客,你想用Gogland去编译其他人的工程,应该不是问题!! Syncthi ...
- Mybatis 优化:
Mybatis 的优化: ** 第一个 对于数据库配置的优化: 创建一个 DB.properties 的文件 里面编写Key = value 形式的数据库信息 比如: driver = com.mys ...
- junit启动tomcat来进行单元测试
1.pom.xml配置下载需要的jar. <dependency> <groupId>junit</groupId> ...
- 爬虫实战2:爬头条网美图--Ajax图片加载处理
完整代码经测试可成功运行,目的是抓取头条网输入街拍后的图片,涉及的知识点如下 1. md5加密使用方法 方法1:不创建实例对象,直接使用 >>> from hashlib impor ...
- pandas.concat连接dataframe
https://blog.csdn.net/stevenkwong/article/details/52528616
- 总结day6 ---- set集合,基本类型的相互转化,编码,数据类型总结,循环时候不要动列表或者字典,深浅copy
python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,比如你利用id ...
- How to manage local libraries in IntelliJ IDEA
如何在 IntelliJ IDEA 中管理本地类库 一般来说,如果项目是基于 Maven 管理工具的,我们会在 pom.xml 中添加 dependency 来管理依赖.但有时也会遇到要用的类库不在 ...
- leetcode-383-Ransom Note(以空间换时间)
题目描述: Given an arbitrary ransom note string and another string containing letters from all the magaz ...
- ASP.NET Core 2.0中的Azure Blob存储
问题 如何在ASP.NET Core中使用Azure Blob存储 解 创建一个类库并添加NuGet包 - WindowsAzure.Storage 添加一个类来封装设置, publicclass A ...