如何在.net核心mvc 3.1中将整个ViewModel传递到外部Javascript文件中?

如何在.net核心mvc 3.1中将整个ViewModel传递到外部Javascript文件中?,javascript,.net,asp.net-mvc,asp.net-core-mvc,Javascript,.net,Asp.net Mvc,Asp.net Core Mvc,我希望将整个ViewModel从.cshtml文件传递到同一个cshtml文件中包含的外部Javascript中 我尝试过不同的解决方案,但都不管用。我首先从cshtml文件中的reguarl js变量开始,并将其传递到外部js文件中。 例如,对于下面的代码,当我点击下面的按钮时,我得到了uncaughtreferenceerror:myValue没有定义 **- in test.cshtml file:** <button onclick="testAlert()&quo

我希望将整个ViewModel从.cshtml文件传递到同一个cshtml文件中包含的外部Javascript中

我尝试过不同的解决方案,但都不管用。我首先从cshtml文件中的reguarl js变量开始,并将其传递到外部js文件中。 例如,对于下面的代码,当我点击下面的按钮时,我得到了uncaughtreferenceerror:myValue没有定义


 **- in test.cshtml file:**

<button onclick="testAlert()"></button>
<script language="text/javascript">
    var myValue = "myValue test";
</script>
<script src="~/js/test.js"></script>

**in test.js:**
/**
 *  This is a test alert function in external js.
 * */
function testAlert() {
    console.log(myValue);
}

我想。。。。你在找什么

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
所以在你的情况下

function testAlert() {
    var customer = @Html.Raw(JsonConvert.SerializeObject(Model.CustomerDetails));
    var customerJson = JSON.parse(customer);
    console.log(customerJson.Names.FirstName);
}

据我所知,如果您想将模型传递给JS脚本,我建议您可以使用
@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(object model))
将模型转换为@Seabizkit建议的Json字符串。然后您可以将此json转换为js中的模型并读取其属性

更多详细信息,请参考以下代码: 模型类:

public class Course
{
    public int CourseID { get; set; }
    public string Title { get; set; }
    public int Credits { get; set; }
    public string SemesterNumber { get; set; }
}
在视图中设置如下按钮:

<input type="button" onclick="testAlert()" value="test" />
@section scripts{

    <script>
         // Notice: we should add '' between the html.raw to set it as a json string.
        var customer = '@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model))';

    </script>
}
结果:


我得到了自己的答案,但还不完全满意,所以直到今天才发布。 不过还需要进一步完善

test.cshtml:

@using Newtonsoft.Json;
<script type="text/cshtml">
viewmodel = @Html.Raw(JsonConvert.SerializeObject(Model));
</script>
只有这样,它才能正常工作。除了额外的初始化部分,与@brando Zang的答案最为相似。仍然不知道为什么它在没有var或let的情况下可以工作

此外,intellisense尚未在我的外部js文件test.js上运行。 但是非常感谢Brando Zang和Sea Bizkut花时间来帮助我

一些对其他人有用的发现:

在默认的.net核心mvc 3.1中,默认的json会自动将viewmodel值从pascal case转换为camelcase,因此使用newtonsoft会保留默认功能和命名约定。 可以在启动时为默认JSon解析器本身执行此操作,但这很麻烦,所以请改用Newtonsoft

  • 同样对于枚举值,默认情况下它接受值而不是字符串。所以在模型中。e、 g在CustomerType的js对象中,您将获得0,1,2,而不是标准、高级或VIP。所以为了解决这个问题

    (下面是枚举的VB语法-无法显示SO中正确缩进的代码) 公共枚举CustomerType 标准=0 保费=1 VIP=2 结束枚举

  • TestModel.cs:

    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    
    [JsonConverter(typeof(StringEnumConverter))]
    public CustomerType CustomerType;
    

    你的代码完全正确,不知道为什么不起作用,though@VyasSenthil,有更新吗?我的回复或Seabizkit回复对你有帮助吗?@BrandoZhang-谢谢你花时间帮助我,还有Seabizkut。非常感谢你的帮助。但请看看我的答案,到底是什么起了作用。如果可能的话,我仍然希望改进它。伙计们,我如何将来自c#对象的注释转换成json字符串,以便在处理外部JS文件时能够对javascript对象进行intellisense?
    @using Newtonsoft.Json;
    <script type="text/cshtml">
    viewmodel = @Html.Raw(JsonConvert.SerializeObject(Model));
    </script>
    
    var viewmodel;
    
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    
    [JsonConverter(typeof(StringEnumConverter))]
    public CustomerType CustomerType;