视图'或者当我运行我的项目时没有找到它的主人

本文关键字:主人 项目 我的 或者 运行 视图 | 更新日期: 2023-09-27 18:14:56

这是控制器

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();
            }
        }
    }
}

my view is

@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>

我得到下面的错误,我尝试了所有的想法,跑出了

Server Error in '/' Application.
The view 'SELECT 
   [Extent1].[ProductId] AS [ProductId], 
   [Extent1].[Prod_Name] AS [Prod_Name], 
   [Extent1].[Unit_Price] AS [Unit_Price], 
   [Extent1].[ListPrice] AS [ListPrice], 
   [Extent1].[Qty_on_Stock] AS [Qty_on_Stock], 
   [Extent1].[Prod_Description] AS [Prod_Description]
   FROM [dbo].[Products] AS [Extent1]' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Product/SELECT 
   [Extent1].[ProductId] AS [ProductId], 
   [Extent1].[Prod_Name] AS [Prod_Name], 
   [Extent1].[Unit_Price] AS [Unit_Price], `enter code here
   [Extent1].[ListPrice] AS [ListPrice], 
   [Extent1].[Qty_on_Stock] AS [Qty_on_Stock], 
   [Extent1].[Prod_Description] AS [Prod_Description]
   FROM [dbo].[Products] AS [Extent1].aspx

我右键单击索引方法并创建了视图,仍然得到这个错误,有人可以帮助我,请我是新的MVC

视图'或者当我运行我的项目时没有找到它的主人

MVC是未能找到适当的视图,因为你传递错误的对象给你的视图。目前,' products '只是db.Products的字符串表示,而您的索引视图需要IEnumerable<Product>

改变
 [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);
        }