视图'索引'或者当我运行我的项目时找不到它的主人

时间:2016-10-25 01:49:38

标签: c# asp.net-mvc

这是控制器

let grad = CAGradientLayer()
grad.colors = [
    UIColor.red.cgColor, // top color
    UIColor.blue.cgColor // bottom color
]
grad.locations = [
    0.0, // start gradating at top of view
    1.0  // end gradating at bottom of view
] 

grad.frame = view.bounds
view.layer.addSublayer(grad)

我的观点是

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>

我右键单击了Index方法并创建了视图但仍然得到此错误可以有人帮助我,请我是MVC的新手

1 个答案:

答案 0 :(得分:2)

当您将错误的对象传递给视图时,MVC无法找到合适的视图。目前,&#39; products&#39;只是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);

        }