3

First i would like to apologize if this is a stupid question. While learning MVC i came a cross creating a strong type views based on model which for example will display all Users by 2 different functions, first will return a View passing a collection of users second will return a collection of JSON objects. so my question is, as i'm not familiar with JSON and for me using Models are more clear, why then using JSON in MVC?

In short why using:

var users = _db.Users
           .Where(r => r.Name.Contains(q))
           .Take(10)
           .Select(r => new { r.Name,r.LastName,r.Address.Country });
        return Json(users, JsonRequestBehavior.AllowGet);

In place of:

 var users= _db.Users
           .Where(r => r.Name.Contains(q))
           .Take(10);
            return View(users);

Maybe this is a bad code example, but why converting Models to Jason before passing them to a view, if we can directly pass the models.

6 Answers 6

2

For the first code snippet, it is more likely that it is used for a ajax call to seamlessly load the data from the server to the client and perhaps keep refreshing it without reloading the page. Also, the Users entity may have much more information than needed by the client, so the first example is reducing the information served and doesn't require you to create a new model to represent it.

The second example would be a controller action actually returning a view and that view is strongly typed.

If you would make a project that uses the WebApi, you wouldn't return views, you would expect the client to ask for data and that the client renders it how it sees fit.

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

Comments

2

If you want the call to return HTML that can be rendered in a browser, you would return a View, which basically takes the data and applies it to an HTML template.

If you want the call to return data only (most likely to be processed by an AJAX request), then you would return JSON.

Comments

1

The first is going to return JSON with a content type of application/json while the latter will return html with the content type text/html (after the appropriate View Rendering Engine has rendered the view in question) - they can be considered two different representations of the same resource. You may want to return JSON in response to a request made via AJAX and return only the data from the model that is required, but you may want to return HTML in response to a "normal" request.

out of the box, ASP.NET MVC does not support Content Negotiation, that is, make a request with the Accept header set to a particular MIME type will likely not yield a response with the expected content type - controller actions will in the majority of cases be configured to return a resource in one particular content type (usually html). ASP.NET Web Api on the other hand does support Content Negotiation out of the box.

Comments

1

JSON (= data only) is generally used in asynchronous (AJAX) requests as they can easily be processed by Javascript, while Views are styled HTML responses ready to be displayed by the browser.

Comments

1

Client side libraries understand JSON very well, so if you want to parse the result and manipulate them using a client side framework/custom scripts, then JSON is a better option (AngularJs is one example).

Comments

0

In Addition to your question, when using JSON, first you convert Model to JSON but when you users post a request you might reconvert JSON to model in order to persist result to DB. :-) I

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.