![](/img/trans.png)
[英]What is the ASP.NET Core MVC equivalent to Request.RequestURI?
[英]What is the equivalent MVC code for ASP.NET CORE?
在我的 MVC C# 項目(不是核心)中,我成功地使用了這個:
索引.cshtml:
<script>
@{
var ListVar = Model.TimeToolDataList;
//TimeToolDataList (*it's an object list, ex: NAME = string, VALUE = int)
var SerializedList = new JavaScriptSerializer().Serialize(ListVar);
}//razer code in the script tag ^ by the @{} wrapper
var SerializedListRefined = JSON.parse('"@SerializedList"');
var FinalList = JSON.parse(htmlDecode(SerializedListRefined));</script>
偉大的。,? 我可以控制台,記錄列表並“查看”它的內容,ASP.NET CORE 項目的此代碼等效於什么? 因為當我復制/粘貼 * 連同所有其他東西時,它不會運行!!!
這不能用: JavaScriptSerializer()
這也不能用: JSON.parse(htmlDecode(SerializedListRefined));
更新,而不是JavaScriptSerializer()
,這有效:
var jsonStr = JsonSerializer.Serialize(ListVar);
^在@using System.Text.Json;
運行時,瀏覽器上的控制台給我一個“未捕獲的 ReferenceError:htmlDecode 未定義”錯誤
try something like the next to deserialize the object correclty you have to use @HtmlRaw
just assuming ListVar
is your Model
/ Object
(the object comming from the c# controller)
您可以執行以下操作:
var jsoninJS = @Html.Raw(Json.Serialize(Listvar));
這會在您的var jsoninJS
中為您提供 object,您可以使用 javascript 中的Console.log(jsoninJS)
打印它。
如果你包括@using System.Web
你也可以用 C# 解碼
使用@HttpUtility.HtmlDecode(string)
但您應該在打印數據時解碼/編碼,而不是其他任何地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.