Continuous PartialView Update Using ASP.NET MVC 3

I am developing a website using ASP.NET MVC 3.

I have a number of sections containing news from different departments, as shown in the image below:

http://i.stack.imgur.com/G21qi.png

Sections are added to the home page using

@Html.Action("_News", "Home", new { id = 1 })

Where id 1 = "Ledelsen", 2 = "Omstillingen", etc.

My controller contains the following action:

[ChildActionOnly]
public ActionResult _News(int id)
{
    // controller logic to fetch correct data from database
    return PartialView();
}

I have CRUD running, but my question is, how can I update PartialView without postback at a given interval?

I suppose I should use Javascript / jQuery for this, but I could not.

Can someone point me in the right direction or better yet, provide an example of how to do this?

Thanks in advance

EDIT: , , , , partialviews

+3
2

div:

<div id="partial1">

javascript , AJAX div:

$("#partial1").load("_News", { id="1"} );

jQuery. . , div, , _News. , javascript.

load() setTimeout():

var t = setTimeout(function () {$("#partial1").load("_News", { id="1"} );}, 60000);

60 . t clearTimeout(t) javascript-, .

. @iko . , cache: false .

var t = setTimeout(function () { $.ajax({ 
                                     url: "_News", 
                                     data: { "id": "1" }, 
                                     type: "POST", 
                                     cache: false, 
                                     success: function (data) { 
                                          $("#partial1").innerHTML = data; 
                                     }}); }, 60000);
+8

, "UpdatePanel" Razor (mvc 3) . , , , .

, [ChildActionOnly], ( 1 2).

script .

<script type="text/javascript">
    setInterval(function()
    {
          $.load('<%= Url.Action("_News", "Home", new { id = Model.Id } ) %>', function(data) {
          $('#partial_'@(Model.Id)').html(data);
      });
    }
    , 30000);
</script>

<div id="partial_@(Model.Id)" />
0

All Articles