asp.net mvc的4剃刀块中的JavaScript会抛出错误 [英] asp.net mvc 4 javascript inside razor block throws error

查看:75
本文介绍了asp.net mvc的4剃刀块中的JavaScript会抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的剃须刀code会抛出错误:

This is my razor code which throws error:

@section script
{
    <script type="text/javascript">
        $(document).ready(function () {
            @if (TempData["Message"] != null)
            {
                showNotification("'" + TempData["Message"].ToString() + "'");
            }
        });
    </script>
}

报告说, showNotification 不存在。它认为这是一个C#code其中这是一个JavaScript函数。有谁请让我知道如何解决这个问题?谢谢!

It says showNotification doesn't exist. It thinks this is a C# code where it's a javascript function. Could anybody please let me know how do I fix this error? Thanks!

推荐答案

抛出一个文本标记周围,因为编译器的东西JavaScript是剃刀语法。当你这样做,你将需要添加一个 @ 来的TempData的电话。

Throw a text tag around it, since the complier things your javascript is Razor syntax. When you do this, you will need to add a @ to the TempData call.

@section script
{
    <script type="text/javascript">
        $(document).ready(function () {
            @if (TempData["Message"] != null)
            {
                <text>showNotification('@TempData["Message"].ToString()');</text>
            }
        });
    </script>
}

这篇关于asp.net mvc的4剃刀块中的JavaScript会抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆