Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

353
Views
.net core, How to user @model inside javascript function in ViewComponent

i have a ViewComponent where i need some server code inside javascript function, ie: get url of a controller action and some Model properties

if i use them got impostaLingua is not defined error

i try to use a global js variable and set it on docReady but got variable not defined in docReady

i cannot write function in Layout.cshtml because there i don't have the server variable

what is the correct way to do it?

Thanks

@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>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!