简体   繁体   中英

ViewData Compilation Error : BC30203: Identifier expected

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.

在此处输入图片说明

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>

将以下行添加到视图页面的顶部,即可正常使用。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM