0

我创建了以下模型对象:-

namespace MvcApplication1.Models

    {
        public class listofpack
        {
           public string packageName { get; set; }
           public string packageId { get; set; }
        }
    }

我在控制器上填充它的值如下: -

public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            using (var client = new WebClient())
            {
                var query = HttpUtility.ParseQueryString(string.Empty);
                query["j_username"] = "kermit";
                query["hash"] = "9449B5ABCFA9AFDA36B801351ED3DF66";
                query["loginAs"] = "admin";
                query["loginAs"] = User.Identity.Name;

                var url = new UriBuilder("http://localhost:8080/jw/web/json/workflow/package/list");
                url.Query = query.ToString();
                string json = client.DownloadString(url.ToString());
                var model = new JavaScriptSerializer().Deserialize<listofpack>(json);
                return View(model);
                // return Content(json, "application/json");
            }

        }

在那之后,我试图通过模型循环如下: -

@model IEnumerable<MvcApplication1.Models.listofpack>
@{
    ViewBag.Title = "Home Page";
}
@foreach(var item in Model) { 

        @Html.ActionLink(item.packageId.ToString(), "about", "Home", new { id = "crm"},null)

}

但是当我运行视图时,我会收到以下错误:-

The model item passed into the dictionary is of type 'MvcApplication1.Models.listofpack', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication1.Models.listofpack]'. 

BR

:::更新:::: - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- -------------------------------------------------- ------------------

我已将控制器代码更新为:-

public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            using (var client = new WebClient())
            {
                var query = HttpUtility.ParseQueryString(string.Empty);
                query["j_username"] = "kermit";
                query["hash"] = "9449B5ABCFA9AFDA36B801351ED3DF66";
                query["loginAs"] = "admin";
                query["loginAs"] = User.Identity.Name;

                var url = new UriBuilder("http://localhost:8080/jw/web/json/workflow/package/list");
                url.Query = query.ToString();
                string json = client.DownloadString(url.ToString());
                var model = new JavaScriptSerializer().Deserialize <List<listofpack>>(json);
                return View(model);
                            }
            }

并在视图中:-

@model List<MvcApplication1.Models.listofpack>
@Model.Count();
@foreach(var item in Model) { 

        @Html.ActionLink(item.packageId.ToString(), "about", "Home", new { id = "crm"},null)

}

但问题是视图中不会显示任何内容,并且.count()将返回零,尽管应该有 5 个 jason 对象传递给视图。那么可能出了什么问题??此致

4

2 回答 2

0
var model = new JavaScriptSerializer().Deserialize<listofpack>(json);

正在反序列化为单个 listofpack 对象。您的观点期望

@model IEnumerable<MvcApplication1.Models.listofpack>

也许尝试这样的事情:

var model = new JavaScriptSerializer().Deserialize<IEnumerable<listofpack>>(json);
于 2012-10-11T13:38:58.077 回答
0

阅读这个问题: 在 JSON 反序列化期间没有为“System.String”类型定义无参数构造函数

No parameterless constructor defined for type of 'System.Collections.Generic.IEnumerable`1[[MvcApplication1.Models.listofpack, MvcApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.

这个异常意味着你使用的泛型“IEnumerable<listofpack>”没有构造函数,因为它是一个接口,所以你应该使用“字典”或“列表”

编辑:这是一个例子:我创建了一个类并将其命名为 Employee:

  public class Employee
{    
    public string lastName { get; set; }
    public string firstName { get; set; }
}

然后我在控制器中写了以下语句:

string jsonEmployees="{'employees': [{ 'firstName':'John' , 'lastName':'Doe' }, { 'firstName':'Anna' , 'lastName':'Smith' }, { 'firstName':'Peter' , 'lastName':'Jones' }]}";
var model = new JavaScriptSerializer().Deserialize<Dictionary<string,List<Employee>>>(jsonEmployees);
return View(model);

所以尝试使用“Dictionary< string,List< listOfpack >>”。

于 2012-10-11T14:34:05.707 回答