0

I'm trying to render partial view with Ajax to fill the <div id="AjaxDiv"></div> element. I'm able to achieve this with jQuery:

<input id="btnCompetitors" type="button" value="Get Competitors" />

<script>
        function loadCategoriesPartialView() {
            $('#AjaxDiv').load(@Url.Action("CategoriesPartial", "Dashboard"));
        }
    </script>

but I cannot get it to work with Ajax.ActionLink, which I would prefer.

    @Ajax.ActionLink("Categories", "CategoriesPartial", "Dashboard", new AjaxOptions()
{
    UpdateTargetId = "AjaxDiv",
    InsertionMode = InsertionMode.Replace
})

What is missing? I tried various additional AjaxOptions, like calling the same method with OnSuccess etc., but I cannot do it properly. Bundles work fine for jquery (basically jquery + unobtrusiveAjax), so hopefully the same ones should work for the Ajax helper.

Turo
  • 1,537
  • 2
  • 21
  • 42

2 Answers2

0

try this

data model

public class DisplayData
{
   public int ID { get; set; }
   public DisplayData(int ID)
   {
          this.ID = ID;
   }
}

Main View(PartialDemo.cshtml)

  @model IEnumerable<MvcApplication5.Models.DisplayData>        
  @{
     ViewBag.Title = "PartialDemo";
   }
  <script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
  <script src="../../Scripts/jquery.unobtrusive.min.js" type="text/javascript"></script>
  @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>

Create a Partial view PartialDemoUC.cshtml

model IEnumerable<dynamic> 

@foreach(var items in Model)
{
   @items.ID   
}

Action Method

1)This will be called the first when you render the view and the time when you press the ajax button

 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);
       }
    } 
maztt
  • 12,278
  • 21
  • 78
  • 153
  • this is much more than I want to achieve here. I want to pass the model to partial views, not to the index view that includes these partial views. But I see interestingly apart from unobtrusive-ajax, you also reference unobtrusive itself. I will try this. – Turo Jan 20 '16 at 09:59
0

It looks like I overlooked the issue, and helpers were rendering correctly. It's jQuery that was wrong. Below the correct one:

<input id="btnCategories" type="button" value="Get Categories" />

<div id="AjaxDiv"></div>

<script>
    $(function () {
        $("#btnCategories").click(function () {
            $("#AjaxDiv").load("@Url.Action("CategoriesPartial", "Dashboard")");
        });
    });
</script>
Turo
  • 1,537
  • 2
  • 21
  • 42