0

(edited)Now I have fixed the previous error but getting the error:

Illegal characters in path.

I'm trying to iterate through some json building an unordered list of links. I have the @for statement in a display template and the Html.DisplayFor in the view. Here's my code:

Controller:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace app.Web.Areas.Admin.Controllers
{
    public class CommonReportsController : Controller
    {
        public ActionResult CommonReports(Guid orgId)
        {
            JObject TopListData = JObject.Parse(System.IO.File.ReadAllText(@"C:\Dev\app\app.Web\json\TopListData.json"));
            string cData = Newtonsoft.Json.JsonConvert.SerializeObject(TopListData);
            var TopListDataView = TopListData;
            return View(TopListDataView);
        }

    }
}

View:

@model app.API.Models.CommonReports.CommonReportsModel
@using app.API.Resources;
@using app.Web.Utility;
@{
    ViewBag.Title = Text.Get(Text.eTextType.Label, @Text.Get(Text.eTextType.Label, "CommonReports_Title"));
}
@section BreadCrumbTitle {
    @( Text.Get(Text.eTextType.Tab, @Text.Get(Text.eTextType.Label, "CommonReports_Title")))
}

<div class="titletop">@ViewBag.Title</div>
<div id="CommonReportsList">
    &nbsp;
    @Html.DisplayFor(m => m.CommonReports, "CommonReportsDisplayTemplate")
</div>

Display Template:

@model app.API.Models.CommonReports.CommonReportsModel
@using app.API.Resources;
<ul class="row-centered-list">
@for (var i = 0; i < 1; i++)
{
<li><img src="~/Images/document.png" />&nbsp;&nbsp;@Html.ActionLink(@Text.Get(Text.eTextType.Label, TopListDataView[i].ListLabel), TopListDataView[i].ListView, TopListDataView[i].ListController, null, new { data_toggle = "tooltip", data_original_title = TopListDataView[i].ListToolTip })<span class="newRed"> - @Text.Get(Text.eTextType.Label, "CommonReports_New")</span></li>
}
</ul><br />

Model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace InRollPlus.API.Models.CommonReports
{
public class CommonReportsModel
{
    public class TopListData
    {
        public string ListLabel
        {
            get;
            set;
        }
        public string ListView
        {
            get;
            set;
        }
        public string ListController
        {
            get;
            set;
        }
        public string ListToolTip
        {
            get;
            set;
        }
    }
    [UIHint("CommonReportsDisplayTemplate")]
    public string CommonReports
    {
        get;
        set;
    }

    public string TopListView
    {
        get;
        set;
    }
    public IList<TopListData> TopListDataView
    {
        get;
        set;
    }
}
}

What am I doing wrong here?

tereško
  • 58,060
  • 25
  • 98
  • 150
Jeff Mcbride
  • 453
  • 2
  • 6
  • 19
  • `Model.TopListDataView[i].ListLabel` but `TopListDataView` is type of `string` so that would make no sense either. Impossible to understand what your really trying to do. –  Dec 03 '15 at 22:44
  • TopListDataView is a json array. How should I reference it then? – Jeff Mcbride Dec 03 '15 at 22:47
  • Nothing makes sense - what is the point of `string cData - ...` in you controller - you never use it (its just thrown away when you do `return View(TopListDataView);` which may as well be `return View(TopListData);`) –  Dec 03 '15 at 22:51
  • cData is actually taking TopListData and deserializing it. I just forgot to use it as such: `var TopListDataView = cData;`. – Jeff Mcbride Dec 03 '15 at 22:57
  • Please give your view a list of strings (your links), instead of serving it as JSON. Then use a foreach loop to create your list of links.. – Repo Dec 03 '15 at 23:36
  • @Repo Not sure I follow? I'll be recieving the Json from another part of the application to build the links and display them in the display template. – Jeff Mcbride Dec 04 '15 at 00:04
  • And you should deserialize it to a .NET object. JsonConvert.DeserializeObject(System.IO.File.ReadAllText(@"C:\Dev\app\app.Web\json\TopListData.json")); . Then work on that =) – Repo Dec 04 '15 at 00:10
  • So as it turns out, not going to need to deal with Json. I'll be getting .net objects so won't need to figure this out. – Jeff Mcbride Dec 04 '15 at 00:27

2 Answers2

1

Try Code On Controller

 public ActionResult CommonReports(Guid orgId)
  { 
     var jsonReader = new StreamReader(Server.MapPath("C:\Dev\app\app.Web\json\TopListData.json"));//Your Json File Path
     var rawJson = jsonReader.ReadToEnd();
     jsonReader.Close();
     var TopListDataView = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TopListData>>(rawJson);//Get List Of all Data Json File And TopListData Name As Model Class
     return View(TopListDataView);
   }
0

So as it turns out, not going to need to deal with Json. I'll be getting .net objects so won't need to figure this out.

Jeff Mcbride
  • 453
  • 2
  • 6
  • 19