将Model对象传递给视图时出错

时间:2012-10-11 13:31:01

标签: asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-4

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

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

::: UPDATE :::: -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----------

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

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对象传递给视图。那可能会出错? 最诚挚的问候

2 个答案:

答案 0 :(得分:0)

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

反序列化为单个listofpack对象。您的观点需要

@model IEnumerable<MvcApplication1.Models.listofpack>

也许可以尝试这样的方法:

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

答案 1 :(得分:0)

阅读这个问题: No parameterless constructor defined for type of 'System.String' during JSON deserialization

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]]'.

此异常表示泛型类型&#34; IEnumerable&lt; listofpack&gt;&#34;你使用过没有构造函数,因为它是一个接口,所以你应该使用&#34; Dictionary&#34;或&#34;列出&#34;

编辑: 这是一个例子: 我创建了一个类,并将其命名为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);

所以尝试使用&#34; Dictionary&lt;串,列表与LT; listOfpack&gt;&gt;&#34;。