In my MVC 3 application, I will have a view that will contain a partial view. The view itself will have a list of dynamically generated links. The link has to cause the partial view to render detailed information for that linked item.

Would I use Ajax for this? If so, since I haven't worked with Ajax before, is there any documentation for using it in a MVC 3 app?

Also when the view is first loaded, the partial view will either not be loaded or ideally show another separate partial view. Any thoughts on a good way of doing this?

Thanks for the help.

link|improve this question

36% accept rate
feedback

2 Answers

up vote 0 down vote accepted

The easiest way of solving this problem that I found was using Ajax helpers that come with the MVC 3 framework. The Ajax video for MVC 3 on Pluralsight did a phenomenal job at succinctly explaining the basics of how to use this feature.

link|improve this answer
feedback

Create an action method which returns a PartialViewResult:

[HttpGet]
public ActionResult DetailedLinkInfo(int someIdentifier)
{
   var detailedLinkInfo = GetFromSomewhere();
   return PartialView(detailedLinkInfo );
}

Then create a partial view, strongly-typed to the type of detailedLinkInfo (let's say it's an DynamicLink.

@model WebApplication.Models.DynamicLink
@* bunch of HTML for the detailed info *@

Then use jQuery on the client-side. Give all your links a class so it makes it easier to hook up the event:

$(function() {
   $('a.dynamic-link').click(function() {
      $.get('/SomeController/DetailedLinkInfo', someIdentifier: $(this).attr('id'), function(data) {
         $('#some-div').html(data);
      });
   });
});

End result: you click one of the links, the jQuery will perform an AJAX GET to your controller action, then bind the result to the div.

HTH

link|improve this answer
Thank you for your response, however i went with the ajax helpers that come with MVC3 – James Apr 29 '11 at 23:15
1  
@James - no problems. You should know though that as of MVC 3, the AJAX helpers and JS library (MicrosoftMvcAjax.js) is considered "legacy" now. jQuery does it all - saves you having an extra JS library. Totally up to you though. – RPM1984 Apr 30 '11 at 1:16
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.