Sam*_*IAm 1 javascript c# asp.net asp.net-mvc razor
我在做:
var url = '@Url.Action("Attachments", "Transactions")';
url += '/?id=' + 3201;
$("#attachments").load(url);
Run Code Online (Sandbox Code Playgroud)
但是,在加载时它什么也不做。我错过了什么吗?
我基本上想调用类似于:
@{Html.RenderAction("Attachments", "Transactions", new { id = 3301 });}
Run Code Online (Sandbox Code Playgroud)
我在控制台上收到以下错误:
http://server:54137/Transactions/@Url.Action(%22Attachments%22,
Run Code Online (Sandbox Code Playgroud)
    您必须使用外部 JavaScript 文件,该文件不会解析您的 razor 语法,因此您的控制台中会出现@Url.Action(%22Attachments%22..
你有几个选择:
创建一个 JavaScript 函数并传入 url:
function loadUrl(url) {
   $("#attachments").load(url);
}
然后在你的剃刀中在脚本标签中调用它:
loadUrl(@Url.Action("Attachments", "Transactions", new { id = @Model.Id })
Run Code Online (Sandbox Code Playgroud)
data方法从 JavaScript 中读取它。在您的剃刀标记中添加以下内容:
<button data-url="@Url.Action("Attachments", "Transactions", new { id = @Model.Id })" />
Run Code Online (Sandbox Code Playgroud)
从您的 JavaScript 事件处理程序中读取它:
var url = $(this).data('url');
$("#attachments").load(url);
Run Code Online (Sandbox Code Playgroud)
我更喜欢第二种选择。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           5902 次  |  
        
|   最近记录:  |