简体   繁体   English

ASP.NET CORE 的等效 MVC 代码是什么?

[英]What is the equivalent MVC code for ASP.NET CORE?

In my MVC C# project (NOT THE CORE) i used this successfully:在我的 MVC C# 项目(不是核心)中,我成功地使用了这个:

Index.cshtml:索引.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>

great.,?伟大的。,? i can console,log the list and 'see' it's contents, what is the equivalent of this code for ASP.NET CORE project?我可以控制台,记录列表并“查看”它的内容,ASP.NET CORE 项目的此代码等效于什么? because when i copy/pasted *along with all the other stuff, it wont run!!!因为当我复制/粘贴 * 连同所有其他东西时,它不会运行!!!

This cant be used: JavaScriptSerializer() This cant be used either: JSON.parse(htmlDecode(SerializedListRefined));这不能用: JavaScriptSerializer()这也不能用: JSON.parse(htmlDecode(SerializedListRefined));

Update, instead of JavaScriptSerializer() , this worked:更新,而不是JavaScriptSerializer() ,这有效:

var jsonStr = JsonSerializer.Serialize(ListVar);

^under @using System.Text.Json; ^在@using System.Text.Json;

when running, the console on the browser gives me an 'Uncaught ReferenceError: htmlDecode is not defined' error运行时,浏览器上的控制台给我一个“未捕获的 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) 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)

you can do the next:您可以执行以下操作:

var jsoninJS = @Html.Raw(Json.Serialize(Listvar));

that gives you in your var jsoninJS the object that you can print with Console.log(jsoninJS) in javascript.这会在您的var jsoninJS中为您提供 object,您可以使用 javascript 中的Console.log(jsoninJS)打印它。

if you include @using System.Web you can also decode with C#如果你包括@using System.Web你也可以用 C# 解码

using @HttpUtility.HtmlDecode(string) but you should decode/encode on you print the data, not anywhere else.使用@HttpUtility.HtmlDecode(string)但您应该在打印数据时解码/编码,而不是其他任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM