1
我想下載一個JSON文件,當我們點擊下載按鈕時。如何使用Underscore.js模板中的數據呈現Url.Action?
public ActionResult getJsonFile(Guid ProtocolId)
{
List<Country> lst = new List<Country>();
for (int i = 1; i <= 10; i++)
{
lst.Add(new Country() { CountryId = i, CountryName = "India" + i });
}
string jsondata = new JavaScriptSerializer().Serialize(lst);
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(jsondata);
return File(bytes, "application/json", "JsonData.json");
}
此代碼正常工作對我來說,如果我不任何一個參數(ProtocolId
)發送到這個方法。
<script type="text/ng-template">
<button type="button" onclick="location.href='@Url.Action("getJsonFile", "Home")">Download</button>
</script>
這裏,protocolid
需要爲我使用Backbone.js的作爲客戶端腳本中使用這些標籤<%= protocolid %>
(Underscore.js模板)的約束。
我怎樣才能綁定protocolid
到錨標記,這樣當用戶點擊鏈接Download
它應該在protocolid
發送到getJsonFile()
方法?
Hi Emile Bergeron,感謝您的解答。我對第一個解決方案做了一些改動,它對我來說工作得很好。 ">Download –