use c# class object in javascript -


i want use object created in c# class in javascript . know can use json library convert server side object json object can use in javascript.

i have downloaded newtonsoft.json library . have following aspx.cs page code

using newtonsoft.json;   public partial class _default : system.web.ui.page {     protected void page_load(object sender, eventargs e)     {         product p = new product();         p.productid = 1;         p.productname = "t-shirt";      }      public class product     {         public int productid { get; set; }         public string productname { get; set; }     } } 

and aspx page using following code javascript access p object value .

    <%@ import namespace="newtonsoft.json" %>  <asp:content id="headercontent" runat="server" contentplaceholderid="headcontent"> <script type="text/javascript">  var jsobject = <%= jsonconvert.serializeobject(p) %>;  function myfunction (){    //work object   alert('hi'); }  </script> </asp:content> 

when try build generate following exception

the name 'p' not exist in current context

i want use p id , name in javascript code.

i have taken reference this answer

the scope of variable p not right.

public partial class _default : system.web.ui.page {     public product p;      protected void page_load(object sender, eventargs e)     {         p = new product();         p.productid = 1;         p.productname = "t-shirt";      }      public class product     {         public int productid { get; set; }         public string productname { get; set; }     } } 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -