2011-12-13 90 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

    相關問題