更新时间:2023-11-29 15:21:16
这应该做的工作:
<script type="text/javascript">
var ShowWelcomeMsg = @Html.Raw(Model.ShowWelcomeMsg);
</script>
您应该先序列化数据。相反,传递一个布尔值,你应该传递一个字符串使用JSON符号:
You should serialize first your data. Instead of passing a boolean, you should pass a string with json notation:
public class IndexViewModel
{
public string ShowWelcomeMsg { get; set; }
}
为了序列化到JSON你应该做的:
In order to serialize to json you should do:
public ActionResult Index()
{
var serializer = new JavaScriptSerializer();
var vm = new IndexViewModel
{
ShowWelcomeMsg = serializer.Serialize(true)
};
return View(vm);
}
这样,你甚至可以序列化整个C#对象,并把它作为在JavaScript中的任何其他对象。
This way you can even serialize a whole C# Object and use it as any other object in JavaScript.