嗨!我尝试学习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