Moamen Ibrahim
Moamen Ibrahim

Reputation: 13

ASP.NET MVC 4 changing contents of div without reloading

I'm new to web development and ASP.NET MVC 4

My question is: Is it possible to replace the content of div tag without needing to refresh the whole page?

If so, what is the best practice of it (best use of MVC 4)?

Upvotes: 1

Views: 2841

Answers (3)

Jani Hyytiäinen
Jani Hyytiäinen

Reputation: 5407

Not 100% sure but if I remember right, jQuery is bundled with MVC4. Correct me if I'm wrong.

// Javascript code
$('#mydiv').load('/Content/html/mySnippet.html');

Would replace the contents of a <div id="mydiv"></div> with the contents of the /Content/html/mySnippet.html.

You can also call an action and return a partial view if you wish to have dynamic content instead of a static html template.

Upvotes: 0

Glenn Vandamme
Glenn Vandamme

Reputation: 1206

In order to refresh partial content of a page, you have to use AJAX. There are plenty of resources available online describing how to implement this in ASP.NET MVC. One of the possibilities is using partial views, on which you can find a good tutorial here. However, if you're comfortable with javascript/jQuery a partial view might be overkill if you're just looking to update one div.

Upvotes: 1

Mark S
Mark S

Reputation: 869

Use javascript and make an ajax call. MVC has a JsonResult for the controller you can use if you like.

Upvotes: 1

Related Questions