传递列表以在 MVC 中查看
Pass a List to View In MVC
我正在将列表值传递到我的视图。但是我收到 "The model item passed into the dictionary is of type 'System.Collections.Generic.List" 的错误。这是我的控制器操作:
public ActionResult Index()
{
Test t = new Test();
List<Customer> myList= t.GetData();
return View(myList);
}
视图如下:
@model List<AsyncActions.Models.Test>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table border="1" cellpadding="4">
@foreach (var row in Model)
{
<tr>
@*<td>@row."CustomerID"</td>
<td>@row."CustomerCode"</td>*@
</tr>
}
这是我的模型:
namespace AsyncActions.Models
{
public class Test
{
public List<Customer> GetData()
{
try
{
Customer c = new Customer();
List<Customer> cst = new List<Customer>();
for (int i = 0; i < 10; i++)
{
c.CustomerID = i;
c.CustomerCode = "CST"+i;
cst.Add(c);
}
return cst;
}
catch (Exception)
{
throw new NotImplementedException();
}
}
}
public class Customer
{
public int CustomerID { get; set; }
public string CustomerCode { get; set; }
}
}
我已经尝试从视图中删除 List<>。当我这样做时,我收到错误 "foreach statement cannot operate on variables of type 'AsyncActions.Models.Test' because 'AsyncActions.Models.Test' does not contain a public definition for 'GetEnumerator"。我已经尝试了很多东西。我检查了这个 link ,但这并没有解决我的问题。我无法找出我错过了什么。非常感谢任何帮助。
您的视图模型应该是
@model List<Customer>
将您的 @model
更改为 @model IEnumerable<Customer>
@model IEnumerable<Customer>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table border="1" cellpadding="4">
@foreach (var row in Model)
{
<tr>
@*<td>@row."CustomerID"</td>
<td>@row."CustomerCode"</td>*@
</tr>
}
您应该将模型的定义更改为:
@model List<Customer>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table border="1" cellpadding="4">
@foreach (var row in Model)
{
<tr>
@*<td>@row."CustomerID"</td>
<td>@row."CustomerCode"</td>*@
</tr>
}
在你看来使用这个应该可以解决问题:
@model List<Customer>
我正在将列表值传递到我的视图。但是我收到 "The model item passed into the dictionary is of type 'System.Collections.Generic.List" 的错误。这是我的控制器操作:
public ActionResult Index()
{
Test t = new Test();
List<Customer> myList= t.GetData();
return View(myList);
}
视图如下:
@model List<AsyncActions.Models.Test>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table border="1" cellpadding="4">
@foreach (var row in Model)
{
<tr>
@*<td>@row."CustomerID"</td>
<td>@row."CustomerCode"</td>*@
</tr>
}
这是我的模型:
namespace AsyncActions.Models
{
public class Test
{
public List<Customer> GetData()
{
try
{
Customer c = new Customer();
List<Customer> cst = new List<Customer>();
for (int i = 0; i < 10; i++)
{
c.CustomerID = i;
c.CustomerCode = "CST"+i;
cst.Add(c);
}
return cst;
}
catch (Exception)
{
throw new NotImplementedException();
}
}
}
public class Customer
{
public int CustomerID { get; set; }
public string CustomerCode { get; set; }
}
}
我已经尝试从视图中删除 List<>。当我这样做时,我收到错误 "foreach statement cannot operate on variables of type 'AsyncActions.Models.Test' because 'AsyncActions.Models.Test' does not contain a public definition for 'GetEnumerator"。我已经尝试了很多东西。我检查了这个 link ,但这并没有解决我的问题。我无法找出我错过了什么。非常感谢任何帮助。
您的视图模型应该是
@model List<Customer>
将您的 @model
更改为 @model IEnumerable<Customer>
@model IEnumerable<Customer>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table border="1" cellpadding="4">
@foreach (var row in Model)
{
<tr>
@*<td>@row."CustomerID"</td>
<td>@row."CustomerCode"</td>*@
</tr>
}
您应该将模型的定义更改为:
@model List<Customer>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table border="1" cellpadding="4">
@foreach (var row in Model)
{
<tr>
@*<td>@row."CustomerID"</td>
<td>@row."CustomerCode"</td>*@
</tr>
}
在你看来使用这个应该可以解决问题:
@model List<Customer>