【问题标题】:How to render a JSON file using C# MVC如何使用 C# MVC 渲染 JSON 文件
【发布时间】:2018-08-08 19:11:55
【问题描述】:

我需要使用 MVC 在 C# 中呈现 JSON 文件。

我写的

在控制器中

public ActionResult Index()
{
    List<string> Title = new List<string>();

    using (StreamReader streamreader = new StreamReader(path))
    {
        var json = streamreader.ReadToEnd();
        Rootobject RO = JsonConvert.DeserializeObject<Rootobject>(json);

        Title = RO.items.Select(x => x.title).ToList();
    }

    return View(Title);
}

在模型中

public class Rootobject
{
    public Item[] items { get; set; }
    public bool has_more { get; set; }
    public int quota_max { get; set; }
    public int quota_remaining { get; set; }
}

public class Item
{
    public string[] tags { get; set; }
    public Owner owner { get; set; }
    public bool is_answered { get; set; }
    public int view_count { get; set; }
    public int answer_count { get; set; }
    public int score { get; set; }
    public int last_activity_date { get; set; }
    public int creation_date { get; set; }
    public int question_id { get; set; }
    public string link { get; set; }
    public string title { get; set; }
    public int last_edit_date { get; set; }
}

public class Owner
{
    public int reputation { get; set; }
    public int user_id { get; set; }
    public string user_type { get; set; }
    public int accept_rate { get; set; }
    public string profile_image { get; set; }
    public string display_name { get; set; }
    public string link { get; set; }
}

可见

@model IEnumerable<ProjectName.Models.Item>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@foreach (var d in Model)
{
    <li>@d.title</li>
}

打开网页后出现错误。我需要列出 JSON 文件的所有标题,但我无法获得该列表。所以我只需要在 html 文件中呈现数据

【问题讨论】:

  • 您正在从 ActionResult 中返回 List,因此,您将对其进行迭代并在 li 元素中使用 var d,因为它已经是 title 的值。还有@Nkosi 所说的。
  • 您在视图中将模型声明为IEnumerable&lt;ProjectName.Models.Item&gt;,但您从控制器返回了一个字符串。这似乎是XY problem。您要达到的最终目标是什么?
  • @Nkosi 我需要做一个标题列表
  • 更新模型以查看IEnumerable&lt;string&gt; 并更新循环。除了列表中的标题之外,您还需要其他内容吗?

标签: c# json asp.net-mvc razor


【解决方案1】:

您在视图中将模型声明为IEnumerable&lt;ProjectName.Models.Item&gt;,但您从控制器返回了一个字符串列表。

将视图中的模型更新为IEnumerable&lt;string&gt; 并更新循环。

在视图中

@model IEnumerable<string>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@foreach (var title in Model) {
    <li>@title</li>
}

如果您想返回更多详细信息,请从控制器返回所需信息。

public ActionResult Index() {
    var items = new List<ProjectName.Models.Item>();

    using (var streamreader = new StreamReader(path)) {
        var json = streamreader.ReadToEnd();
        Rootobject RO = JsonConvert.DeserializeObject<Rootobject>(json);

        items = RO.items.ToList();
    }

    return View(items);
}

并相应地更新视图

例如。

@model IEnumerable<ProjectName.Models.Item>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<ul>
@foreach (var item in Model) {
    <li>
    <h4>@item.title</h4>
    @foreach (var tag in item.tags) {
        <p>@tag</p>
    }
    </li>
}
</ul>

【讨论】:

  • 好的 .. 但是如果我需要在视图中包含更多信息,例如标签 ....
  • 这很完美,但我认为你必须写 @tag 而不是 tag ...我错了吗?
  • @HTMLMan 是的,这是我的错字。
猜你喜欢
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多