@for循环遍历MVC 4中的json

时间:2015-12-03 22:38:02

标签: arrays json asp.net-mvc asp.net-mvc-4 display-templates

(已编辑)现在我修复了上一个错误,但收到了错误:

  

路径中的非法字符。

我正在尝试迭代一些json构建一个无序的链接列表。我在显示模板中有<div ng-repeat="item in shifts"> <div ng-show="item.PublishedEmployee.Id == currentId"> Matched </div> </div> 语句,在视图中有@for。这是我的代码:

控制器:

Html.DisplayFor

查看:

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

    }
}

显示模板:

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

型号:

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

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

尝试控制器上的代码

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

答案 1 :(得分:0)

事实证明,不需要处理Json。我将获得.net对象,所以不需要弄明白。