C# 视图';指数';或者在我运行项目时找不到它的主程序
这是控制器C# 视图';指数';或者在我运行项目时找不到它的主程序,c#,asp.net-mvc,C#,Asp.net Mvc,这是控制器 namespace DemoforTesting.Controllers { public class ProductController : Controller { DefaultConnection db = new DefaultConnection(); // GET: Product [HttpGet] public ActionResult Index() {
namespace DemoforTesting.Controllers
{
public class ProductController : Controller
{
DefaultConnection db = new DefaultConnection();
// GET: Product
[HttpGet]
public ActionResult Index()
{
var products = db.Products.ToString();
return View(products);
}
// GET: Product/Details/5
[HttpGet]
public ActionResult Details(int id)
{
return View();
}
// GET: Product/Create
public ActionResult Create()
{
return View();
}
// POST: Product/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Product/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Product/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Product/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Product/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
我的观点是
@model IEnumerable<OnLineShoppingCart.Models.Product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Prod_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Unit_Price)
</th>
<th>
@Html.DisplayNameFor(model => model.ListPrice)
</th>
<th>
@Html.DisplayNameFor(model => model.Qty_on_Stock)
</th>
<th>
@Html.DisplayNameFor(model => model.Prod_Description)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Prod_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Unit_Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.ListPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty_on_Stock)
</td>
<td>
@Html.DisplayFor(modelItem => item.Prod_Description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ProductId }) |
@Html.ActionLink("Details", "Details", new { id=item.ProductId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ProductId })
</td>
</tr>
}
</table>
</body>
</html>
我右键单击了索引方法并创建了视图,但仍然出现此错误。请有人帮助我,我是MVC新手,因为您将错误的对象传递到视图,MVC无法找到适当的视图。目前,
products
“只是db.products
的字符串表示,而您的索引视图需要一个IEnumerable
改变
[HttpGet]
public ActionResult Index()
{
var products = db.Products.ToString();
return View(products);
}
到
[HttpGet]
public ActionResult Index()
{
var products = db.Products.ToString();
return View(products);
}
[HttpGet]
public ActionResult Index()
{
var products = db.Products.ToList(); //Or some other IENUM
return View(products);
}