use*_*779 2 c# asp.net-mvc razor
我正在为MVC做一个简单的(我认为)教程.唯一的问题是教程使用Razor,我需要使用ASPX.我正在看的工作使用它.
我花了几个小时查看互联网的可能性,但我仍然收到此错误:
编译错误
描述:编译服务此请求所需的资源时发生错误.请查看以下特定错误详细信息并相应地修改源代码.
编译器错误消息:CS1061:'object'不包含'Name'的定义,并且没有扩展方法'Name'接受类型'object'的第一个参数可以找到(你是否缺少using指令或程序集引用?)来源错误:第12行:
第13行:
第14行:餐厅:<%:Model.Name%>
第15行:评级:<%:Model.Rating%>
第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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
如果要使用强类型视图,则必须View Model在此行中定义页面:
Inherits="System.Web.Mvc.ViewPage<TheViewModel>" %>
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下它应该是;
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<RestaurantReview>" %>
Run Code Online (Sandbox Code Playgroud)
如果你没有像你那样指定任何东西,那么ViewModel它将是对象,并且如你所知,它没有Name或Rating属性.
| 归档时间: |
|
| 查看次数: |
467 次 |
| 最近记录: |