我创建了以下模型对象:-
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 对象传递给视图。那么可能出了什么问题??此致