jquery - How can I automatically update a partial view? -
i'm trying convert 1 of mvc pages automatically updates data every 10 seconds. here method calls "main" view.
public actionresult index() { return view("someview", new someviewmodel { rows = getsomeinfo() }); } here someview
@html.partial("someview",model) and partial view consumes model
@model databaseconnectionmonitor.models.testdbmodel @foreach(var row in @model.rows)...... now how wire partial view automatically refreshes? there lot of (what appears outdated) methods of ahceiving this, , want follow current best practice. research telling me should using jquery unobtrusive ajax this.
your question vague/broad, vague/broad answers.
if want code examples recommend provide code you've tried can fix it. stack overflow not place go "how do complex thing? show me code." there many possible solutions.
i'm going show 3 of (probably many) possible techniques.
- difficulty: how difficult think new technique.
- durability: (probably bad name) how easy maintain/extend/modify. how reusable , rough estimation of how solid is.
1. use signalr
difficulty: high, durability: high
signalr .net library ideal integrating asp.net mvc applications. allows send information server-side code client or clients in real-time. meant section of page not refresh based on time, rather on arbitrary event.
this technique popular in modern, complex web applications such facebook. in such examples, pages minor-refreshes or updates when new data incoming.

2. use javascript , jquery
difficulty: low, durability: low
javascript has setinterval() function. give function callback , timeout period (in milliseconds) arguments , call function every x milliseconds.
you can use in conjunction jquery's ajax api every x seconds, jquery call mvc partial view asynchronously , replace section of page specified partial view. example:
setinterval(function() { $.ajax({ url: "@url.action("action", controller)" }).success(function(data) { $("#someelement").html(data); }); }, 10000); 3. use web application javascript framework
difficulty: medium, durability: high
web application frameworks such angularjs (my choice), backbone.js, knockout , on better ways achieve goal on jquery. basic idea similar jquery solution more robust.
you need create logic (usually in view-model or controller) responsible synchronising arbitrary data (such html partial view) on user's page. can done timers - included in framework.
Comments
Post a Comment