1

I want to use a object which is created in c# class in javascript . I know i can use json library to convert my server side object to JSON object so that it can be use in javascript.

I have downloaded the Newtonsoft.Json library for this . i 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 for aspx page i am using following code for javascript to access that 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 with object
  alert('Hi');
}

</script>
</asp:Content>

when i try to build this its generate following exception

The name 'p' does not exist in the current context

i just want to use p id and name in my javascript code.

I have taken initially reference from this answer

1
  • p is the name of the variable, which you're calling jsobject in your JS. You should be able to use jsobject.productId and jsobject.productName if I understood correctly how this library works. Commented May 22, 2013 at 6:55

2 Answers 2

2

The scope of your variable p is 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; }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Website finally runs thanks but now in console following error is coming .Uncaught TypeError: Cannot read property 'ProductId' of null . I understand its means p is null but don't have idea why its null ?
how does the resulting html look like?
Do you mean rendered page HTML ?
there is no json object in rendered html ?
i have made an sample project based upon your code. pre rendered it looks like: <pre><code> <script> var jsobject = <%= JsonConvert.SerializeObject(p) %>; alert(jsobject.productName); </script></pre></code> And rendered: <pre><code> <script> var jsobject = {"ProductId":1,"productName":"t-shirt"}; alert(jsobject.productName); </script> </code></pre>
|
1

You can not access local variables of a function in scriptlets define a public property or data member.

public product p {get; set;}
protected void Page_Load(object sender, EventArgs e)
{    
    p = new product()
    p.ProductId = 1;
    p.productName = "t-shirt";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.