简体   繁体   中英

Why Using JSON in MVC application?

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.

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.

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.

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.

JSON(=仅数据)通常用于异步(AJAX)请求,因为它们可以通过Javascript轻松处理,而视图则是样式化的HTML响应,可供浏览器显示。

客户端库非常了解JSON,因此如果要解析结果并使用客户端框架/自定义脚本对其进行操作,那么JSON是更好的选择(AngularJs就是一个例子)。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM