Step 1:
I will again be using DisplayData class in my demo. Here is it.
public class DisplayData
{
public int ID { get; set; }
public DisplayData(int ID)
{
this.ID = ID;
}
}
Step 2:
Create a PartialDemo page
@model IEnumerable<MvcApplication5.Models.DisplayData>
@{
ViewBag.Title = "PartialDemo";
}
@Ajax.ActionLink("Click 1", "PartialDemo", "PartialDemo", new {Data= "1" }, new AjaxOptions { UpdateTargetId = "rsvpmsg" })
@Ajax.ActionLink("Click 2", "PartialDemo", "PartialDemo", new {Data= "2" }, new AjaxOptions { UpdateTargetId = "rsvpmsg" })
<div id="rsvpmsg">
@{ Html.RenderPartial("PartialDemoUC", this.Model);}
</div>
Step 3:
Create a User Control PartialDemoUC.cshtml
@model IEnumerable<dynamic>
@foreach(var items in Model)
{
@items.ID
}
Step 4:
In a PartialDemo controller create an Action method for rendering the partial view on the first call to the method and then on an ajax call(on the click of the link)
public ActionResult PartialDemo(string Data)
{
List<DisplayData> Display = new List<DisplayData>();
if (Request.IsAjaxRequest())
{
if (Data == "1")
{
Display.Add(new DisplayData(3));
Display.Add(new DisplayData(4));
}
else
{
Display.Add(new DisplayData(5));
Display.Add(new DisplayData(6));
}
return PartialView("PartialDemoUC",Display);
}
else
{
Display.Add(new DisplayData(1));
Display.Add(new DisplayData(2));
return View("PartialDemo", Display);
}
}
thanks!!!
ReplyDeletethanks
ReplyDeleteMany examples like this on the internet are too complicated to follow without their whole project.
ReplyDeleteThis was simple and easy to follow from scratch.
Great work, thanks.
This is the best simple example I've found anywhere. Thanks -RaoulRubin
ReplyDeleteThis is what I was looking for about 2 hours,
ReplyDeletekudos
Thanks!! It was so useful :)
ReplyDeleteThank you!
ReplyDeletethx
ReplyDelete