2011-12-13 89 views
1

嗨!我尝试学习Asp.net MVC.i创建了一个示例应用程序来学习它。但按F5返回服务器错误。我在哪里犯了一个错误?哪里有问题?有一个导航栏首页/关于/产品。如果我按产品,错误返回给我。为什么我无法从MVC访问新产品列表?

enter image description here 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> 

回答

3
<%: Html.ActionLink("Product", "ProductDetail", "Product")%> 

动作名称产品详情是不是在你的控制器,你有GETALL动作名称

u需要

public ActionResult ProductDetail() 
    { 
     using (var db = new NorthwindEntities()) 
     { 
      return View(db.Products.ToList()); 
     } 
    } 
0

假设您在执行默认路由。

  1. 问题是您的控制器被命名为ProductsController(复数)。将名称更改为ProductController或将视图文件夹的名称更改为Products

  2. 的另一个问题是,你的控制器不包含ActionMethod ProductDetail, 您在site.Master指向链接ProductController.ProductDetail

如果您想了解Asp.NET的mvc我建议你检查出以下教程。

希望这有助于

+0

我这样做你说什么。但结果是一样的:

  • <%:Html.ActionLink(“Test”,“Detail”,“Product”)%>
  • Penguen

    +0

    是的,因为您没有Detail ActionMethod。目前你的Controller只有一个叫做“GetAll”的方法。 – dknaack

    2

    你能否提供更多的信息,如实际的服务器错误?

    快速查看代码视图名称是错误的。 去/产品/期望一个名为Products而不是产品细节的视图。

    0

    首先,尝试更具体。你会得到什么错误?

    现在,我想我明白了什么是问题,但我只是猜测,因为你没有发布错误。对于我所看到的,您可以传递给IQueryable:ctx.Products而不枚举它。

    然后视图试图枚举它,但上下文被处置,所以查询无法运行。我想你得到的错误就像“上下文不可用”或类似的东西。

    首先,尝试更改像这样的语句:ctx.Products.ToList()并查看错误是否消失。

    我发现你使用字典的另一件事,而使用强类型的ViewModel会好得多(或至少使用动态)。

    0

    如果不指定视图名称,应用程序将寻找一个视图与你的方法的名称。 I.e:

    public ActionResult GetAll() 
        { 
         using (var ctx = new MyDbEntities()) 
         { 
          ViewData["Products"] = ctx.Products; 
          return View(); 
         } 
    
        } 
    

    正在寻找“Product/GetAll.aspx”的视图。解决方案:

    • 变化的方法
    • 变化的观点
    • 名的名称做return View("ProductDetail");
    1

    指定视图名称有控制器名称和视图之间的不匹配文件夹名称。也在动作名称和视图名称之间。

    产品控制器 - >产品文件夹中查看

    GETALL操作 - >ProductDetail.aspx查看

    重命名为:

    产品控制器 - >产品视图中的文件夹

    产品详情操作 - > ProductDetail.aspx查看

    而且,让这个行:

    ViewData["Products"] = ctx.Products; 
    

    这样的:

    ViewData["Products"] = ctx.Products.ToList(); 
    

    你的观点不应该打电话到数据库。

    +0

    我这样做你说什么。但结果相同:

  • <%:Html.ActionLink(“Test”,“Detail”,“Product”)%>
  • Penguen

    +0

    @Penguen应该是:

  • <%:Html.ActionLink(“Test”,“产品详细信息“,”产品“)%>
  • 您可以发布您收到的异常吗? – epignosisx

    相关问题