8

I have a view page

my view page

<div id="beReplaced">
    @Ajax.ActionLink("please click on me to bring the partial view",
                     "PatrialViewToBeCalled",
                     new AjaxOptions()
                        {UpdateTargetId = "beReplaced",
                        InsertionMode = InsertionMode.InsertAfter,
                        HttpMethod="Get",
                        LoadingElementId = "prgress" })
 </div>

i have a Controller

public PartialViewResult PatrialViewToBeCalled()
{
    var customer = db.Customers.First();

    return PartialView("PartialViewThatMustBeShow",customer);
}   

but when i click on the generated link it brings me to a new page instead of replacing or appending the partial view to the div tag.

What's the problem?

tereško
  • 56,151
  • 24
  • 92
  • 147
Amir Jalali
  • 2,950
  • 4
  • 30
  • 43

5 Answers5

10

It could have been that you were missing the:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

In the main view. This tells the main view to recognize the ajax helper.

PreethaA
  • 825
  • 12
  • 13
1

I found the solution. The problem was due to the corrupted unobtrusive-ajax.min.js file.

Amir Jalali
  • 2,950
  • 4
  • 30
  • 43
0

If you are working with ASP .NET MVC 4 make sure that

  @Scripts.Render("~/bundles/jquery")

is in the end of the body tag as well.

      @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html> 
DmitryBoyko
  • 32,983
  • 69
  • 281
  • 458
0

I've just been working on it for too long, thinking it did not work. Viewing the page source didn't show anything either.

Eventually, I found out it actually did work when I saw the successful requests in the console of Firebug. It only was showing it off screen. In the HTML tab of Firebug I finally found the output, even though it does not show up in the page source.

Br2
  • 115
  • 9
0

If you have jQuery loaded into your page, why not use the simple jquery get / load method to get the partial view ?

<div id="beReplaced">
  <a href="#" id="lnk1">please click on me to bring the partial view</a>
</div>

Javascript

$("#lnk1").click(function(){
$.get('/controller/PatrialViewToBeCalled', function(data) {
         $('#beReplaced').html(data);
      });
});
Shyju
  • 197,032
  • 96
  • 389
  • 477
  • Because i'm trying to keep my view clean and make it as small as possible and Ajax helpers in MVC have the power to make complex Statements. – Amir Jalali Jan 08 '12 at 15:33
  • 1
    If i want to keep my views clean, i would stick with unobtrusive principle.I feel this jQuery approach is much cleaner than giving your ajax parameter methods inside the view (where i would like to see only html markup only as much as i can) – Shyju Jan 08 '12 at 16:27
  • Probably because he wanted to know how to use ActionLink, not how to manipulate the DOM using standard jQuery? :/ – Ted Nyberg Mar 22 '13 at 11:45