【问题标题】:Error when passing Model object to my view将模型对象传递到我的视图时出错
【发布时间】:2012-10-11 13:31:01
【问题描述】:

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

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 对象传递给视图。那么可能出了什么问题?? 最好的问候

【问题讨论】:

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


【解决方案1】:
var model = new JavaScriptSerializer().Deserialize<listofpack>(json);

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

@model IEnumerable<MvcApplication1.Models.listofpack>

也许可以尝试以下方法:

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

【讨论】:

  • 使用 IEnumerable 将引发以下错误:- 没有为 'System.Collections.Generic.IEnumerable`1[[MvcApplication1.Models.listofpack, MvcApplication1, Version=1.0. 0.0,文化=中性,PublicKeyToken=null]]'。
  • 听起来你的模型是空的。
【解决方案2】:

阅读这个问题: 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]]'.

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2017-04-26
    相关资源
    最近更新 更多