有没有可能在asp-mvc4中使用ajax呈现的视图中包含javascript

Is it possible to include javascript in view rendered with ajax in asp mvc 4?

本文关键字:视图 javascript 包含 ajax 有可能 asp-mvc4      更新时间:2023-09-26

我在尝试用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)&nbsp;@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了。谢谢