[RESOLVED]what is Json.Encode() usaged in mvc

when we call any server side function through jquery ajax and when server side function return data then data is serialize automatically in json format….so why we need to use

Json.Encode(ViewBag.Faculties)

 

//Get all faculties with the help of model
// (ViewBag.Faculties), and convert data into json format.
var allFaculties = @Html.Raw(Json.Encode(ViewBag.Faculties));

 
i never work with mvc but i worked with webform for long time and have seen that when we call server side function by jquery ajax then i always get result in json format by default.
 
in mvc data is not serialize in json format automatically ?
 
if action method return json result set then what happen? do we need to use Json.Encode() in case when action method return type is json resultset ??
 
please share your knowledge. thanks

mou_inn

var allFaculties = @Html.Raw(Json.Encode(ViewBag.Faculties));

I believe the above code is written in the javascript. You are trying to generate javascript code in the view. In this javascript code you are setting the JSON. As you are generating the javascript code, you need to convert your object (ViewBag.Faculties)
to string. This where you need to use the Json.Encode

mou_inn

if action method return json result set then what happen? do we need to use Json.Encode() in case when action method return type is json resultset ??

If your action method is returning a json object, then you don’t need to convert the object to string using the Json.Encode.

Converts the json object to text.

From MSDN: http://msdn.microsoft.com/en-us/library/system.web.helpers.json.encode(v=vs.111).aspx

Call the Encode method to convert a data object (for example, a set of data records that you return from a database query) to a string that is encoded in JSON format. You typically convert data objects to JSON format in order to work with the data as text
in a web page by using JavaScript. This lets you create a page where users interact with the data in the browser without requiring round trips to the server.

Hi mou_inn,

First, for Json.Encode is used to convert Object to string, so, if you need the JSON string, you could use it, if you just need the JSON Object, you don’t need convert to string.

mou_inn

if action method return json result set then what happen? do we need to use Json.Encode() in case when action method return type is json resultset ??

That’s base on your requirement, if you want to get the JSON string, you need to convert object to string.

On the other hand, there are the corresponding function in jQuery.

Best Regards

Starain

what Html.Raw() does ?

Hi mou_inn,

That is used to encode the data to HTML.

@Html.Raw("<div>value</div>")

Best Regards

Starain

mou_inn

what Html.Raw() does ?

Html.Raw does not encode the Html. It generates the string as it is. Other Html helpers encodes the Html to prevent security attacks. You need to be careful if you are using Html.Raw.

programmers are generally pretty sloppy with security. with html sites, script injection is common, so MVC make an attempt to limit it. so razor performs html encoding of any output strings. so:

   @myvar
   @Html.DisplayFor(m=>m.myProp)

perform html encoding when rendering their output. sometimes you need to defeat this encoding, say when the variable is an actual html string, or javascript code as in your example. then you use @Html.Raw() to prevent encoding.

note: the json.encode trick works because json is valid javascript syntax for object literals.

U guys could show how data object look like before json.encode and how things look like after json.encode. Hope you will answer with right example.

Leave a Reply