2

I am facing problem while using the object that i have sent from my controller to view using Json.

I am sending List of Objects to the View by using NewtonSoft.Json 7.x.x to serialize the List to Json. I am using the below code to serialize:

return JsonConvert.SerializeObject(DataToSend, Formatting.None, new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        });

I have 2 Entity Classes: 1) Form 2) FormFields

There is a 1-to-Many Relationship between these 2 entities.

public class Form
{
    public long ID { get; set; }
    public string Name { get; set; }
    public virtual List<FormField> FormFields { get; set; }
}

public class FormField
{
    public long FormID { get; set; }
    public string FieldLabel { get; set; }
    public string FieldType { get; set; }
    public string FieldValue { get; set; }
    public virtual Form form { get; set; }
}

I am trying to send the List of FormField to the view for rendering using Javascript. I am able to send it using the above serializaton Method.

But the problem is that, When i receive the Array of Objects in Javascript. It has Json Reference object IDs. I am not able to access those objects normally.

I am able to render the 1st FormField value in that array but i am not able to render rest of them. It is coming as Undefined.

I am attaching a screenshot of JSON Object Values which i am receiving on UI. You can see that there is an Object array. Each Object should have the Object of Type FormField and should have that field's value but there isn't.

Only the Object at index 0 is having the values and Rest of the Indexes have only Reference IDs.

Json Object Values

Please help me resolve it.

Thanks

2
  • Can you give the example of the serialized json object you get in the view? What do you mean by Json Reference object IDs. Commented Sep 1, 2015 at 18:19
  • sure, I'll Update my post now with the example json data....Thanks for responding.... Commented Sep 2, 2015 at 6:42

1 Answer 1

1

I don't know, if it is the best solution or not but I am posting so that may be it can save someone else's struggle to find the solution.

I Managed to do it using the .NET extension library plugin. Instead of using Newtonsoft.Json, I used System.Web.Extension DLL.

I put the attribute ScriptIgnore in my models as:

public class Form
{
    public long ID { get; set; }
    public string Name { get; set; }

    [ScriptIgnore]
    public virtual List<FormField> FormFields { get; set; }
}

public class FormField
{
    public long FormID { get; set; }
    public string FieldLabel { get; set; }
    public string FieldType { get; set; }
    public string FieldValue { get; set; }

    [ScriptIgnore]
    public virtual Form form { get; set; }
}

Then, I serialized it with the System.Web.Script.Serialization.JavaScriptSerializer as:

return new JavaScriptSerializer().Serialize(DataToSend);

It will serialize all the data but it will ignore the Properties which are having the attribute ScriptIgnore and it won't try to serialize them. For me it worked as required.

Just came to know that there is an Attribute JsonIgnore provided by NewtonSoft.Json that does the same job as ScriptIgnore. So, If you are using NewtonSoft Library then you can also use this.

It's good to know there are more ways to achieve the same result ;)

Sign up to request clarification or add additional context in comments.

1 Comment

The only thing is the thing marked as the answer looks like c#, not Javascript, which I thought is the language of the original request.

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.