开发者

Using Jquery in Controller Page-ASP.NET MVC-3

开发者 https://www.devze.com 2023-03-07 12:14 出处:网络
Could any one give an example, how to use Jquery开发者_C百科 in Controller Page. MVC3 -ASP.NET(How To put various tags like )

Could any one give an example, how to use Jquery开发者_C百科 in Controller Page. MVC3 -ASP.NET(How To put various tags like )

I want to show a simple alert before rendering a view in Controller.

Thank you. Hari Gillala


Normally scripts are part of the views. Controllers shouldn't be tied to javascript. So inside a view you use the <script> tag where you put javascript. So for example if you wanted to show an alert just before rendering a view you could put the following in the <head> section of this view:

<script type="text/javascript">
    alert('simple alert');
</script>

As far as jQuery is concerned, it usually is used to manipulate the DOM so you would wrap all DOM manipulation functions in a document.ready (unless you include this script tag at the end, just before closing the <body>):

<script type="text/javascript">
    $(function() {
        // ... put your jQuery code here
    });
</script>

If you are talking about rendering partial views with AJAX that's another matter. You could have a link on some page that is pointing to a controller action:

@Html.ActionLink("click me", "someAction", null, new { id = "mylink" })

and a div container somewhere on the page:

<div id="result"></div>

Now you could unobtrusively AJAXify this link and inject the resulting HTML into the div:

$(function() {
    $('#mylink').click(function() {
        $('#result').load(this.href, function() {
            alert('AJAX request finished => displaying results in the div');
        });
        return false;
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消