Skip to content Skip to sidebar Skip to footer

Accessing Mvc Server Side Variable In Javascript

I'm moving one of our web applications from Drupal to an ASP.net MVC Web Application. One of the Drupal functions gets some data from a web service and converts it to a JS Array, a

Solution 1:

You can use Strongly Typed view to create cshtml page

  • if you want to access JSON object

    View1.cshtml @model mvcApplication1.Models.model1 @{ var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); serializer.MaxJsonLength = int.MaxValue; var jsonModel = serializer.Serialize(Model); } var JsonData = @Html.Raw(jsonModel); // declare a javascript variable and use it

  • if you want to access server variable at cshtml page just use @ to access server-side variables after getting the value you can use javascript variable in another js file

    Declare your javascript variable outside $(document).ready(function() {}

    or before using the variable

you can use that variable in Javascript file.

Post a Comment for "Accessing Mvc Server Side Variable In Javascript"