2015-12-03 4 views
-1

(Edited) Maintenant, j'ai corrigé l'erreur précédente, mais obtenir l'erreur:boucle @For itérer JSON dans MVC 4

Illegal characters in path.

Je suis en train de parcourir certains JSON la construction d'une liste non ordonnée de liens. J'ai l'instruction @for dans un modèle d'affichage et la Html.DisplayFor dans la vue. Voici mon 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); 
     } 

    } 
} 

Vue:

@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> 

Modèle d'affichage:

@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 /> 

Modèle:

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

Qu'est-ce que je fais mal ici?

+0

'Model.TopListDataView [i] .ListLabel' mais' TopListDataView' est un type de 'string' donc cela n'aurait aucun sens non plus. Impossible de comprendre ce que vous essayez vraiment de faire. –

+0

TopListDataView est un tableau json. Comment devrais-je le référencer alors? –

+0

Rien n'a de sens - quel est le point de 'chaîne cData - ...' dans votre contrôleur - vous ne l'utilisez jamais (il est juste jeté lorsque vous renvoyez View (TopListDataView); 'qui peut aussi bien être' retour Voir (TopListData); ') –

Répondre

0

Alors il se trouve, ne va pas avoir besoin de faire face à JSON. Je vais recevoir des objets .net donc je n'aurai pas besoin de comprendre cela.

1

Essayez Code Sur contrôleur

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