ViewData Compilation Error : BC30203: Identifier expected

时间:2015-06-15 14:48:11

标签: javascript c# jquery html asp.net-mvc

I have code below.

    public ActionResult PatrList(decimal PAT_ID)
    {
        ViewData["PAT_ID"] = PAT_ID;
        return View(); 
    }

<script type="text/javascript">
    $(document).ready(function () {
        var PAT_ID = '<%= ViewData["PAT_ID"].ToString() %>';
        $("body").data("PAT_ID", PAT_ID);
    });
</script>

Unfortunately, I got Compilation Error : BC30203: Identifier expected.

enter image description here

2 个答案:

答案 0 :(得分:1)

Try the following:

<script type="text/javascript">
    $(document).ready(function () {
        var PAT_ID = '<%= (ViewData["PAT_ID"]).ToString() %>';
        $("body").data("PAT_ID", PAT_ID);
    });
</script>

or better in Razor engine:

<script type="text/javascript">
    $(document).ready(function () {
        var PAT_ID = '@(ViewData["PAT_ID"]).ToString()';
        $("body").data("PAT_ID", PAT_ID);
    });
</script>

答案 1 :(得分:1)

将以下行添加到视图页面的顶部,然后它可以完美运行。

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>