有没有可能在asp-mvc4中使用ajax呈现的视图中包含javascript
Is it possible to include javascript in view rendered with ajax in asp mvc 4?
我在尝试用asp-mvc4在部分视图中包含javascript时遇到了严重的问题,阅读这篇文章时,我按照说明创建了一个自定义的Html帮助程序来包含js。不幸的是,要么我从布局中调用它,然后从未按时调用实际包含Javascript的方法(这意味着在我执行ajax请求时已经调用了页面请求),要么我试图将它包含在我的部分视图中,但部分视图似乎会自动删除其中编写的任何Javascript。所以我尝试渲染一个标准视图,但即使这样也没有解决问题。那么,问题是,是否有可能在asp-mvc中用ajax调用的视图中呈现javascript?有人做了吗,请给我建议。实际上,我需要在我的"ajax调用"视图中运行javascript,因为我必须检测该视图中表单中的值。我不会发布Helper的代码,因为它完全来自引用的帖子。谢谢
好的,我会更具体地说明为什么我认为我需要在我的特定视图中使用javascript。以下是主视图的代码:
@model MvcCursosSSP.Models.CursosModel
@using System.Threading;
@{
ViewBag.Title = "AgregarHorario";
Layout = "~/Views/Shared/_Layout.cshtml";
int numhoras = ViewBag.NumeroHoras;
int dias = ViewBag.Dias;
System.Globalization.CultureInfo culture = Thread.CurrentThread.CurrentCulture;
}
<h2>AgregarHorario</h2>
<table class="horario">
<colgroup>
<col id="col1">
<col id="col2">
<col id="col3">
<col id="col4">
<col id="col5">
<col id="col6">
</colgroup>
<caption>@if (ViewBag.Modulo != null)
{
<text>Modulo </text>@ViewBag.Modulo[0].NumModulo.ToString()<br />@ViewBag.Modulo[0].Nombre.ToString()<br />
}Horario del @Model.FechaInicio.Day.ToString() de @Model.FechaInicio.ToString("MMMM", culture) al @Model.FechaTermino.Day.ToString() de @Model.FechaTermino.ToString("MMMM", culture)</caption>
<thead>
<tr><th>Horario</th>
@for (int i = 0; i <= @dias; i++)
{
<th>@Model.FechaInicio.AddDays(i).ToString("dddd", culture) @Model.FechaInicio.AddDays(i).Day.ToString()</th>
}
</tr>
</thead>
<tbody>
@for (int i = 0; i <= @numhoras; i++)
{
var newTime = @Model.HorarioInicio.Add(new TimeSpan(i, 0, 0));
<tr><td>@newTime.Hours:00</td>@for (int j = 0; j <= @dias; j++)
{
<td id="@newTime.Hours" rel="@Model.FechaInicio.AddDays(j).Day.ToString()"><span class="addHorario">AgregarHorario</span></td>
}
</tr>
}
</tbody>
</thead>
</table>
@section scripts {
<script type="text/javascript">
$('span.addHorario').click(function () {
var td = $(this).parent('td');
td.load("/Cursos/CreateMateria/" + new Date(@Model.FechaInicio.Year.ToString(), @Model.FechaInicio.Month.ToString(), td.attr('rel')).toISOString().substring(0,10) + '/' + td.attr('id'));
});
$('button[id^="save"]').click(function () {
horaini = $('#horarioInicio'+$(this).attr('id').substring(4, $(this).attr('id').length)).val();
horafin = $('#horarioFin'+$(this).attr('id').substring(4, $(this).attr('id').length)).val();
alert(parseInt(horafin)-parseInt(horaini));
});
</script>
}
但是最后一个以$('button[id^="save"]').click
开头的javascript不起作用,我认为这是因为dom已经构建好了,jquery无法识别任何内容。有人能提出解决方案吗?
Nevermind,我解决了它。看起来Jquery已经有了解决方案,只需在加载中添加一个回调函数,如下所示:
$('span.addHorario').click(function () {
var td = $(this).parent('td');
td.load("/Cursos/CreateMateria/" + new Date(@Model.FechaInicio.Year.ToString(), @Model.FechaInicio.Month.ToString(), td.attr('rel')).toISOString().substring(0,10) + '/' + td.attr('id'), function(){
$('button[id^="save"]').click(function () {
horaini = $('#horarioInicio'+$(this).attr('id').substring(4, $(this).attr('id').length)).val();
horafin = $('#horarioFin'+$(this).attr('id').substring(4, $(this).attr('id').length)).val();
alert(parseInt(horafin)-parseInt(horaini));
});
});
});
现在我可以操纵局部视图的dom了。谢谢
相关文章:
- 如何使用javascript从主svg对象动态创建svg视图框
- 如何包含特定于每个视图angularjs的javascript文件
- 如何在Ionic Android中将Javascript注入到web视图中
- 如何在MVC5中ajax调用的部分视图中引用外部javascript
- 在CodeIgniter视图中将Javascript或jQuery代码作为PHP文件编写可以吗
- 我应该将MVC视图特定的javascript文件放在哪个文件夹中
- 树视图javascript的反向级别
- node.js/javascript/couchdb视图到关联数组似乎不起作用
- 将c#视图模型转换为javascript模型时转义HTML标记
- 如何访问Web2py'中python代码中的JavaScript变量;s视图
- Rails:将资产管道中的纯javascript限制为特定视图
- 使用javascript在MVC视图中解析.net JSON字符串
- Android网络视图:JavaScript在触摸时停止
- 如何在mvc3视图javascript中查找当前请求是否为ajax
- MVC视图Javascript变量
- MVC4 部分视图 JavaScript 捆绑问题
- IE9 兼容性视图 Javascript 问题
- playframework从视图javascript调用Model方法
- IOS:Web视图javascript禁用
- IE 11和Chrome中的网格视图Javascript错误