2012-02-04 27 views
2

我正在爲MVC做一個簡單的(我認爲)的教程。唯一的問題是教程使用Razor,我需要使用ASPX。我正在看的工作使用它。我在做什麼錯誤將Razor轉換爲ASPX?

我花了幾個小時尋找在互聯網上的可能性,但我仍然得到這個錯誤:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'object' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) Source Error:

Line 12:
Line 13:
Line 14: Restaurant: <%: Model.Name %>
Line 15: Rating: <%: Model.Rating %>
Line 16:

控制器代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using EdeToFood.Models; 

namespace EdeToFood.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 

      var model = new RestaurantReview() 
      { 
       Name = "Tersiguil's", 
       Rating = 9 
      }; 

      return View(model); 
     } 

     public ActionResult About() 
     { 
      ViewBag.Location = "Maryland, USA"; 
      return View(); 
     } 
    } 
} 

的ASPX是:

%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Home Page 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2><%: ViewBag.Message %></h2> 
    <p> 
     To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>. 
    </p> 

    <div > 
    Restaurant: <%: Model.Name %> 
    Rating: <%: Model.Rating %> 

    </div> 
</asp:Content> 
+0

你缺少'<'在aspx頁面的開始,但它可能是一個複製粘貼問題。請參閱下面的答案。 – gdoron 2012-02-05 00:14:34

+0

我只讀了問題標題,但聽起來像是一切。 – smartcaveman 2012-06-06 17:45:09

+0

我們在哪裏可以找到教程? – SamekaTV 2014-01-07 10:32:13

回答

5

如果你想使用強類型的意見你要定義頁面的View Model在這條線:

Inherits="System.Web.Mvc.ViewPage<TheViewModel>" %> 

所以你的情況是應該的;

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<RestaurantReview>" %> 

如果你像你一樣不指定任何內容,ViewModel將對象,如你所知,它沒有NameRating性能。

+1

非常感謝。它像一個魅力。 – user803779 2012-02-05 02:15:57

0

我相信你不想冒號作爲腳本分隔符的一部分:

<h2><% ViewBag.Message %></h2> 

您還可以使用不同的語法與平等作爲一個快捷方式Response.Write()簽收,如

<%=DateTime.Now %> 

Source

+0

冒號指定腳本分隔符的輸出應爲HTML編碼 – 2012-02-07 14:06:49

+0

謝謝,我沒有意識到這一點 – kaveman 2012-02-07 18:51:29

相關問題