2011-09-22 34 views
1

我想要使用jquery獲取視圖的html,並希望在另一個視圖頁面上的div中顯示此html。
我知道jQuery的.html()方法,但它不適合我的要求。我做了很多搜索,但沒有找到。請儘快發送答案。如何在ASP中使用jQuery獲取Html視圖MVC 3.0

+0

你能告訴我們你到目前爲止所嘗試過的嗎? – PeeHaa

回答

0

有一個正常的ActionResult呈現的局部

呼叫使用jquery AJAX例如該ActionLink的

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

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

您可以使用jQuery load加載從操作方法

有這個標記在您查看其中有DIV響應

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

在您的中使用名爲YourAction的acion方法這樣

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

控制器確保你的你有Views/Your文件夾,裏面有你想要顯示的一些標記下的視圖YourAction.cshtml

相關問題