[转]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 ...
随机推荐
- css+html+JQuery 万能弹出层,居中显示
function ShowMsg(str) {//要提示的文字 $(".payment_time_mask").remove(); $("body").appe ...
- Dapper.Contrib.Extensions问题
Dapper.Contrib.Extensions问题 Dapper.Extension.1.0.0.1\lib\net45\Dapper.Extension.dll Dapper.Contrib.1 ...
- PHP设计超级好用的文件上传处理类一 (37)
<?php class FileUpload { private $filepath; //指定上传文件保存的路径 private $allowtype=array('gif', 'jpg', ...
- eclipse如何汉化,把eclipse改成中文版
eclipse默认是英文版的,对于中国人来说使用英文语言的软件是件痛苦的事情.下面我来详细说一下如何把eclipse改成中文版的. 工具/原料 eclipse英文版 eclipse中文插件 方法/ ...
- [Swift]遍历字符串
Swift中无法再使用传统形式的for循环. //传统for循环形式不适用于Swift for(单次表达式;条件表达式;末尾循环体){中间循环体:} 字符串遍历方法1:使用该indices属性可以访问 ...
- Leetcode 88 合并两个有序数组 Python
合并两个有序数组 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分 ...
- Dubbo 自定义异常,你是怎么处理的?
前言 记录Dubbo对于自定义异常的处理方式. 实现目标 服务层异常,直接向上层抛出,web层统一捕获处理 如果是系统自定义异常,则返回{"code":xxx,"msg& ...
- Centos7 自定义systemctl服务脚本
序言篇: 之前工作环境一直使用Centos6版本,脚本一直在使用/etc/init.d/xxx:系统升级到Cento7后,虽然之前的启动脚本也可以使用,但一直没有使用systemctl 的自定义脚本. ...
- LTE:EPC
User Identifiers - IMSI and GUTI IMSI A globle id that unique identifies a subscribe.It composed t ...
- 在微信移动端input file拍照或从相册选择照片后会自动刷新页面退回到一开始网站进入的页面
<input type="file" accept="image/*"/> 调用打开摄像头后,聚焦后拍照,点击确认,这时页面会出现刷新动作,然后回退 ...