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);
3
  • 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 Commented Apr 4, 2013 at 9: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. Commented Apr 4, 2013 at 9:36
  • Check here Commented Apr 4, 2013 at 12:24

3 Answers 3

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);


});
Sign up to request clarification or add additional context in comments.

1 Comment

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
0

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

$(".div_class_name").html(result);

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.