简体   繁体   English

将json返回到Core 2中的部分视图

[英]Return json to partial view in core 2

I would like to create a view that would contain a different view. 我想创建一个包含其他视图的视图。 I've never used json before. 我以前从未使用过json。 How i can do this and How can I format the json data in the view? 我该怎么办?如何在视图中格式化json数据?

My first function "Details" is to retrieve a object from the database and return view "Details.cshtml". 我的第一个功能“ Details”是从数据库中检索对象并返回视图“ Details.cshtml”。 In this view I want generates a partial view ("Stats.cshtml"). 我要在此视图中生成局部视图(“ Stats.cshtml”)。 And now I want to generate a partial view with the data downloaded in the json format inside the Stats function. 现在,我想使用Stats函数中的json格式下载的数据生成局部视图。

Controller 调节器

public IActionResult Details(int? id = 1)
{
  var person = _context.Persons.Find(id);

  return View(champion);
}

public IActionResult Stats()
{
  var json = new WebClient().DownloadString("url");

  return Json(s);
}

View - Details.cshtml 查看-Details.cshtml

@model Person

<div class=row">
  <div class="col-sm-5"> @Model.Name </div>
  <div class="col-sm-5"> @Html.Partial("Stats") </div>
</div>

View - Stats.cshtml 查看-Stats.cshtml

<h2>Stats</h2>
<div> here I want to put in a json field </div>

When I run "Stats" function from the address localhost/Home/Stats I get the result in json, but when I run "Details" function I get view "Details" and "Stats" without the json value. 当我从localhost / Home / Stats地址运行“统计信息”功能时,会在json中得到结果,但是当我运行“详细信息”功能时,会得到没有json值的视图“详细信息”和“统计信息”。

to render a partial, you have many options, by your code, 要渲染部分图片,您可以通过代码选择多种方式,

  1. the simplest one is: move your Stats code to Details action 最简单的方法是:将您的统计信息代码移至“详细信息”操作

     public ActionResult Details() { ...//prepare your person viewModel var result = new WebClient().DownloadString("url"); var stats = JsonConvert.DeserializeObject<YourViewModel>(result); //you have 2 options to return data yourPersonModel.Stats=stats ; //<== you have to change PersonViewModel //or ViewBag.Stats=stats; return View(yourPersonModel); } 

then in Details.cshtml: 然后在Details.cshtml中:

@Html.Partial("Stats", ViewBag.Stats or Model.Stats)//by your choice before.
  1. Since Html.Action is removed, but ViewComponent comes in Core, you cannot directly call it right now, but this link will tell you how to make it back: @Html.Action in Asp.Net Core 由于已删除Html.Action,但ViewComponent随Core一起提供,因此您现在不能直接调用它,但是此链接将告诉您如何将其恢复: Asp.Net Core中的@ Html.Action

     public ActionResult Stats() { var result = new WebClient().DownloadString("url"); var yourViewModel = JsonConvert.DeserializeObject<YourViewModel>(result); return PartialView(yourViewModel); } 

add the following code in your View - Stats.cshtml: 在您的View-Stats.cshtml中添加以下代码:

@model YourViewModel

then in Details.cshtml: 然后在Details.cshtml中:

@Html.Action("Stats")

Be aware that Html.Action cannot call async actions, be careful to use it. 请注意, Html.Action无法调用async操作,请谨慎使用它。

  1. the next solution is to use new feature ViewComponent, here is details: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1 下一个解决方案是使用新功能ViewComponent,这是详细信息: https ://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1

  2. the last one will not be what you expected: use AJAX to load this partial page on page Details is loaded to client 最后一个不是您期望的:使用AJAX加载页面上的此部分页面详细信息已加载到客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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