开发者

How to get Html of a view using jQuery in ASP .NET MVC 3.0

I want to get html of a view using jquery and want to show this html in a div on another view page.

I know the .html() method of jquery but it doesn't work for my requirement. I have done a lot of searches but I didn't find this. Please send the answer as开发者_Go百科 soon as possible.


have a normal ActionResult that renders a partial

call that ActionLink using jquery ajax e.g.

[HttpPost]
public ActionResult partialview()
{
//do some process if you want to
return PartialView();
}

in jquery

$.ajax({
url:'/Controller/partialview'
type:'POST',
cache:false,
success:function(data){// this handler is called upon successful completion of request

  //data contains the html generated by partial view
  // you can append it to a div or body as you like e.g
  $("#DivID").html(data);
  //$(data).appendTo("body"); 
  //etc
  //etc
},
error:function(jxhr){// error callback is called if something goes wrong
  if(typeof(console)!='undefined'){
   console.log(jxhr.status);
   console.log(jxhr.responseText);
    }
 }
});


You can use jQuery load to load the response from an action method

Have this markup in your View which has the DIV

<script type="text/javascript">  
 $(function(){
       $("#yourDivID").load("@Url.Action("YourController","YourAction")");
 });
</script>

Have an acion method called YourAction in your YourController controller like this

public ActionResult YourAction()
{
  return View();
}

Make sure your you have the view YourAction.cshtml under Views/Your folder, which has some markup you want to display.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜