嗨!我嘗試學習Asp.net MVC.i創建了一個示例應用程序來學習它。但按F5返回服務器錯誤。我在哪裏犯了一個錯誤?哪裏有問題?有一個導航欄首頁/關於/產品。如果我按產品,錯誤返回給我。爲什麼我無法從MVC訪問新產品列表?
Cotroller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models.Db;
namespace MvcApplication1.Controllers
{
public class ProductsController : Controller
{
//
// GET: /Products/
public ActionResult GetAll()
{
using (var ctx = new MyDbEntities())
{
ViewData["Products"] = ctx.Products;
return View();
}
}
}
}
查看:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Db.Product>" %>
<%@ Import Namespace="MvcApplication1.Models.Db" %>
<%@ Import Namespace="MvcApplication1.Controllers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ProductDetail
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ProductDetail</h2>
<ul>
<% foreach (var m in (IEnumerable<Products>)ViewData["Products"])
{ %>
<li><%= m.ProductName %></li>
<% } %>
</ul>
</asp:Content>
的Site.Master:
<div id="menucontainer">
<ul id="menu">
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<li><%: Html.ActionLink("About", "About", "Home")%></li>
<li><%: Html.ActionLink("Product", "ProductDetail", "Product")%></li>
</ul>
</div>
我這樣做你說什麼。但結果是一樣的:
是的,因爲您沒有Detail ActionMethod。目前你的Controller只有一個叫做「GetAll」的方法。 – dknaack