所以我创建了一个在MVC 4中测试DI的项目。是的,这很简单。我有一个看起来像这样的控制器。我的视图如何知道使用MVC返回什么类型的数据?
public class CompanyManagementController : Controller
{
private ICompanyService companyService;
public CompanyManagementController(ICompanyService companyService)
{
this.companyService = companyService;
}
public ActionResult Index()
{
return View(companyService.GetCurrentCompanies());
}
}
我弄不清楚的是如何告诉视图Index.cshtml,我有这个List,我回到它。当我只有一个POCO时很容易。我这样做
[HttpGet]
public ActionResult Create()
{
var company = new Company();
return View(company);
}
[HttpPost]
public ActionResult Create(Company company)
{
using(var context = new InventoryContext())
{
context.Companies.Add(company);
context.SaveChanges();
}
return View("Create");
}
这是非常容易的,因为我刚才说
@model MvcInventoryManager.Models.Company
在Create.cshtml文件
。
任何提示?我想要做的是能够遍历返回公司的列表。
谢谢!
你发送一个列表视图和y你不确定'模型'?如果是这样,那就把模型改成IEnumerable'。 '@model IEnumerable ' –
Buildstarted