2013-04-10 110 views
0

我試圖撥打另一個頁面,以接收該頁面上的代碼。當我這樣做時,第一次打電話JSON.cshtml它工作正常,但撥打JSON2.cshtml時,它不返回任何內容。怎麼會這樣?Jquery .load做第一次加載,但不是第二次

Default.cshtml

@{ 
Layout = "~/_Layout_Main.cshtml"; 
} 

@section head{ 
<script> 
    $(".btn").click(function() 
    { 
     get_update(); 
    }); 

    function get_update() 
    { 
     $("#success").load("JSON2.cshtml"); 
    }; 

    function get_contact() 
    { 
     $("#success").load("JSON.cshtml"); 
    }; 
</script> 
} 

<div id="success"></div> 

<script> 
get_contact(); 
</script> 

<a href="#" class="btn" style="color: #fff;">btn</a> 

JSON.cshtml

<p style="color: #fff;"> 
Hello 
</p> 

JSON2.cshtml

@{ 
for(int i = 0; i > 10; i++) 
{ 
    <p style="color: #fff;"> 
     @i 
    </p> 
    <br /> 
} 
} 
+1

因爲你下載直接觀看,而不是執行在ASP.NET MVC框架的背景下一個動作。您需要創建一個Action來返回此JSON2.cshtml視圖,然後纔會執行代碼。 – 2013-04-10 16:26:49

+0

@ Moby'sStuntDouble你能告訴我一個如何做到這一點的例子嗎? – 2013-04-10 16:34:49

回答

2

真的很簡單。您將需要創建一個控制器:在你看來/ jQuery的

public class YourController: Controller 
{ 
    public ActionResult JSON2() 
    { 
     return View() 
    } 
} 

然後:

$("#success").load('@Url.Action("JSON2", "YourController")'); 

確保您的控制器的名稱的文件夾的名稱相匹配您的JSON2.cshtml視圖坐在,這個視圖將被框架找到。

我會謙卑地認爲,你必須通過這裏的入門教程一看:http://www.asp.net/mvc

相關問題