Can ViewData be referenced in a script block in ASP.NET MVC?

I would like to add a click handler for a button using jQuery, but the handler must refer to the data provided by the controller. Is there a way to access ViewData in a script block in ASP.NET MVC? Or do I need to build a script in the controller?

+1


source to share


1 answer


If the script block is in an ASP.NET page, you can link to it ...

eg:



<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>

      

When a page is rendered by the runtime engine, everything in the <% ...%> block is evaluated regardless of its location on the page.

+2


source







All Articles