.net
  • asp.net-mvc
  • jquery
  • 2013-01-09 31 views 0 likes 
    0

    在.Net MVC 3我尝试运行一个脚本..该脚本是在局部视图。如果我调用Html.RenderAction部分它的作品,但如果我通过jQuery的ajax调用页面中的部分视图它不工作。脚本不加载,如果部分视图通过jQuery的调用ajax

    管窥

    <script type="text/javascript"> 
        document.write("<script src='http://ad.reklamport.com/rpgetad.ashx?tt=t_iddaa_habersayfalari_mac_detay_300x250&ciid=&rnd=" + Math.random() % 99999999 + "'></" + "script>");    
    </script> 
    

    查看

    它的工作原理..

    <html> 
        <head>   
    
        </head> 
        <body>   
         @{Html.RenderAction("Partial");} 
    
        </body> 
    
    
    </html> 
    

    它不工作...

    <body>   
         @{Html.RenderAction("Partial");} 
         <script type="text/javascript"> 
          $.ajax({ 
           type: "post", 
           url: "/Home/Partial", 
           dataType: "html",     
           success: function (result) {      
            $("#content").html(result); 
           }, 
           error: function (result) { 
           } 
          }); 
         </script> 
         <div id="content"> 
    
         </div> 
        </body> 
    

    我需要使用第二种方法..问题是什么?

    回答

    0

    这是因为<div id="content">尚未加载到dom。试着将JS脚本内容的div

    <body>   
         @{Html.RenderAction("Partial");} 
    
         <div id="content"> 
    
         </div> 
    <script type="text/javascript"> 
          $.ajax({ 
           type: "post", 
           url: "/Home/Partial", 
           dataType: "html",     
           success: function (result) {      
            $("#content").html(result); 
           }, 
           error: function (result) { 
           } 
          }); 
         </script> 
        </body> 
    

    后,也可以wrapp您的Ajax请求文档准备事件处理程序

    <script type="text/javascript"> 
          $(function{ 
           $.ajax({ 
            type: "post", 
            url: "/Home/Partial", 
            dataType: "html",     
            success: function (result) {      
             $("#content").html(result); 
            }, 
            error: function (result) { 
            } 
           }); 
          }); 
    </script> 
    
    +0

    我试过页的通话AJAX脚本结束,但它仍然没有工作。 –

    +0

    仍然没有工作:( –

    +0

    如果我只写在脚本警报(“你好世界”)它的工作..我认为脚本不加载? –

    相关问题