1

I need to render a JSON file in C# using MVC.

I wrote

In Controller

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);
}

In Model

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; }
}

In View

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

I got an error once I open the web page. I need to list all title of a JSON file, but I cannot get the list. So all I need is to render the data in an html file

4
  • You're returing a List<string> from your ActionResult, therefor, you would iterate that and just use var d in your li elements as it is already the value of title. Also what @Nkosi said. Commented Feb 28, 2018 at 19:52
  • You declare the model as IEnumerable<ProjectName.Models.Item> in the view yet you return a string from the controller. This appears to be an XY problem. What is the ultimate goal you are trying to achieve? Commented Feb 28, 2018 at 19:53
  • @Nkosi I need to do a list of titles Commented Feb 28, 2018 at 19:56
  • Update model in view to IEnumerable<string> and update loop as well. Do you need anything other than title from list? Commented Feb 28, 2018 at 19:57

1 Answer 1

1

You declare the model as IEnumerable<ProjectName.Models.Item> in the view yet you return a list of strings from the controller.

Update model in view to IEnumerable<string> and update loop as well.

In View

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

If you want to return more details then return the desired information from the controller.

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);
}

and update the view accordingly

For example.

@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>
Sign up to request clarification or add additional context in comments.

3 Comments

Ok .. but what should I do if I need to include more information in the view such as the tags ....
it's perfect but I think you have to write @tag not tag ... am I wrong?
@HTMLMan Yeah that was a typo on my part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.