代码之家  ›  专栏  ›  技术社区  ›  tvanfosson

是否可以在ASP.NET MVC中的脚本块中引用ViewData?

  •  1
  • tvanfosson  · 技术社区  · 17 年前

    我想使用jQuery为按钮添加一个单击处理程序,但该处理程序需要引用控制器提供的数据。在ASP.NET MVC中是否有访问脚本块中的ViewData的方法?或者我需要在控制器中构造脚本吗?

    1 回复  |  直到 17 年前
        1
  •  2
  •   Kieron    17 年前

    如果脚本块位于ASP.NET页面内,则可以引用它。。。

    例如:

    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('#group-edit-form').validate({
                rules: {
                    title: {
                        required: true,
                        remote: '<%=Url.Action("ValidateGroupName", new { id = ViewData["GroupId"] }) %>?parentId=' + getParentId()
                    }
                },
                messages: {
                    title: {
                        required: getMessage (7002),
                        remote: '<%= ((MessagingModel)ViewData["Messages"]).GetMessage (9001) %>'
                    }
                }
            })
    
        });
    </script>