传递一个简单的 IEnumerable 来查看并使用 foreach 循环返回一个空白屏幕? [英] Passing a simple IEnumerable to view and using foreach to loop through returns a blank screen?
本文介绍了传递一个简单的 IEnumerable 来查看并使用 foreach 循环返回一个空白屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的客户模型类列表,我将它传递给我的视图.我想遍历客户类,但我的观点告诉我通过返回一个空白屏幕来解决问题.请告诉我这里有什么问题?
I have a simple List of Customer model class which I am passing to my view. I want to iterate through the customer class but my view is telling me to bugger off by returning a blank screen. Please tell me what is wrong here?
模型类:
public class Customer
{
public string CustomerName { get; set; }
public int Age { get; set; }
}
家庭控制器:
public ActionResult Index()
{
List<Customer> customers = new List<Customer>();
Customer customer = new Customer() { FullName = "MrA" ,Age=25};
Customer customer2 = new Customer() { FullName = "MrB", Age = 125 };
return View(customers);
}
查看 HTML 文件
@model IEnumerable<ASPMVC_Database.Models.Customer>
/...some unrelated code
@foreach( var item in Model)
{
@item.FullName <br/>
}
上面这段简单的代码什么也没给我.白屏而已.我在这里错过了什么?
This simple code above gives me nothing. Just a white screen. What am I missing out here?
谢谢
推荐答案
您永远不会将新客户添加到 customers
,您只需将它们实例化.请尝试以下操作:
You never add the new customers to customers
, you simple instantiate them. Try the following instead:
public ActionResult Index()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { FullName = "MrA", Age = 25});
customers.Add(new Customer { FullName = "MrB", Age = 125 });
return View(customers);
}
这篇关于传递一个简单的 IEnumerable 来查看并使用 foreach 循环返回一个空白屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文