0

I just started working on ASP .NET MVC3 project. I want to render a partial view (*.cshtml file) within javascript/AJAX and put it in a division, Can anybody help me with sample code for that. Thanks in advance.

I tried this way, but not working.

var result = '<%= Html.RenderPartial("_Partialview"); %>';
$(".div_class_name").html(result);
Pavan
  • 1,023
  • 2
  • 12
  • 25
  • Why do you need doing it with AJAX/JavaScript? Why can't you just do it with a helper? I mean just write <%= Html.RenderPartial("_Partialview"); in your view – Alex Ovechkin Apr 04 '13 at 09:30
  • I want this partial page to be rendered onClick event on a image. So i call javascript method for OnClick; this will render partial view and write it on a particular division. – Pavan Apr 04 '13 at 09:36
  • **[Check here](http://stackoverflow.com/a/15793019/2007801)** –  Apr 04 '13 at 12:24

3 Answers3

1

Create a method that returns partial view in Controller class, than call that method with ajax get.

$.get('controller/getpartial', function(data) {

  $('.div_class_name').html(data);


});
  • Thanks for the reply. I did that through controller as you said above, it works fine. But I don't want to call a controller for this task. Is it possible? pls help – Pavan Apr 04 '13 at 09:26
0

create a action and return partial view in string with json and then use

$(".div_class_name").html(result);
Amit
  • 15,217
  • 8
  • 46
  • 68
0
$('#trigger').click(function () {
$.ajax({
  url: '@Url.Action("Method", "Controller")', 
  success: function(data){
    $('#targetDIV').html(data);
  }
    });
    });

So when you will click for example on a trigger button this ajax call will be executed and the partial view returned will be placed to your targetDIV.

arnoldrob
  • 813
  • 3
  • 9
  • 15