ASP.NET MVC 3 Partial View dynamically rendered and linked from dynamic list in view

耗尽温柔 提交于 2019-12-03 21:26:29

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!