在MVC视图中调用JS函数

时间:2016-03-19 19:01:37

标签: javascript c# angularjs asp.net-mvc

在下面的CSHTML文件中,三个javascript函数无效。我尝试了三种不同的方法,但没有一种方法可以正常工作。

我的问题可能是误解了页面加载的时间。我在cshtml文件中访问了c#中的数据库,我想将检索到的所有数据添加到js文件中的数组中,以便将其应用于Angularjs Smart-Table插件。我无法找到任何智能表从数据库中绘制数据的例子(有点愚蠢,他们没有一个)所以我一直试图通过对ASP.net的有限理解来实现它,Angular .js,MVC6和JS。

CSHTML文件

<body ng-controller="paginationCtrl">
Number of Results: @num_results <br> @error_message <br> @results[1].title

@foreach (Event row in results)
{
    <script type="text/javascript">
        jk();
    </script>

    @Html.Raw("push('" + @row.event_id + "', " + "'" + @row.title + "', " + "'" + @row.datetime + "', " + "'" + @row.place + "', " + "'" + @row.address + "', " + "'" + @row.send_calendar_invites + "', " + "'" + @row.full_description + "', " + "'" + @row.description + "', " + "'" + @row.is_expired + "', " + "'" + @row.rsvp_limit + "', " + "'" + @row.rsvp_cutoff + "');");

    <text>push(@row.event_id, @row.title, @row.datetime, @row.place, @row.address, @row.send_calendar_invites, @row.full_description, @row.description, @row.is_expired, @row.rsvp_limit, @row.rsvp_cutoff);</text>
}

Javascript文件

angular.module('myApp', ['smart-table'])
.controller('paginationCtrl', ['$scope', function (scope) {

    scope.itemsByPage = 10;

    scope.rowCollection = [];
    function jk() {
        alert("TEST");
    }
    function createEvent(eid, ttl, dt, plc, addr, sci,
                    fdesc, desc, ie, rl, rc)
    {
        var event_id = eid, title = ttl, datetime = dt, place = plc,    address = addr, send_calendar_invites = sci, full_description = fdesc,  description = plc, is_expired = ie, rsvp_limit = rl, rsvp_cutoff = rc;

        return {
            event_id: event_id,
            title: title,
            datetime: datetime,
            place: place,
            address: address,
            send_calendar_invites: send_calendar_invites,
            full_description: full_description,
            description: description,
            is_expired: is_expired,
            rsvp_limit: rsvp_limit,
            rsvp_cutoff: rsvp_cutoff
        };
    }

    function push(eid, ttl, dt, plc, addr, sci,
                    fdesc, desc, ie, rl, rc) {
        jk();
        scope.rowCollection.push(
            createEvent(eid, ttl, dt, plc, addr, sci,
                    fdesc, desc, ie, rl, rc));
    }

1 个答案:

答案 0 :(得分:0)

你的函数jk只能在控制器中工作,因为你在那里定义。