首页 > 解决方案 > .net 核心,如何在 ViewComponent 的 javascript 函数中使用 @model

问题描述

我有一个 ViewComponent,我需要在 javascript 函数中使用一些服务器代码,即:获取控制器操作的 url 和一些模型属性

如果我使用它们得到 impostaLingua 未定义错误

我尝试使用全局 js 变量并将其设置在 docReady 但变量未在 docReady 中定义

我无法在 Layout.cshtml 中编写函数,因为我没有服务器变量

正确的方法是什么?

谢谢

@model UI.WebApp.Models.MyModel

@(Html.DevExtreme().Lookup()
    .DataSource(Model.LingueSupportate)
    .ID("ddlLingua")
    .Value(Model.LinguaCorrente.TwoLetterISOLanguageName)
    .OnSelectionChanged("function(e) {impostaLingua(e);}")

<script>
var a;
docReady(function () {
    a = @Model.LinguaCorrente;
});

function impostaLingua(e) {
    if(e.selectedItem.Value != @Model.LinguaCorrente)
        document.location.href = '@Url.Action("ImpostaLingua", "Master")?culture=' + e.selectedItem.Value '&returnUrl=' + @Model.PercorsoPagina;
}
</script>

标签: javascriptasp.net-mvc.net-coreasp.net-core-viewcomponent

解决方案


推荐阅读