【问题标题】:What am I doing wrong converting Razor to ASPX?我在将 Razor 转换为 ASPX 时做错了什么?
【发布时间】:2012-02-04 23:57:29
【问题描述】:

我正在为 MVC 做一个简单的(我认为)教程。唯一的问题是教程使用Razor,我需要使用ASPX。我正在寻找的工作使用它。

我花了几个小时在互联网上寻找可能性,但我仍然收到此错误:

编译错误

描述:编译所需资源时出错 服务这个请求。请查看以下内容 具体错误详情并适当修改您的源代码。
编译器错误消息:CS1061:“对象”不包含定义 对于“名称”并且没有扩展方法“名称”接受第一个参数 可以找到“对象”类型的(您是否缺少 using 指令或 程序集引用?)源错误:

第 12 行:
第 13 行:
第 14 行:餐厅:
第 15 行:评级:
第 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>

【问题讨论】:

  • 您在 aspx 页面的开头缺少 &lt;,但这可能是复制粘贴问题。请参阅下面的答案。
  • 我只看了问题的标题,但听起来什么都有。
  • 在哪里可以找到教程?

标签: c# asp.net-mvc razor


【解决方案1】:

如果你想使用强类型视图你必须在这一行定义页面的View Model

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

所以在你的情况下应该是;

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

如果您不指定任何内容,ViewModel 将成为对象,并且众所周知,它没有 NameRating 属性。

【讨论】:

    【解决方案2】:

    我相信您不希望冒号作为脚本分隔符的一部分:

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

    您还可以使用带有等号的不同语法作为Response.Write() 的快捷方式,例如

    <%=DateTime.Now %>
    

    Source

    【讨论】:

    • 冒号指定脚本分隔符的输出应该是HTML Encoded
    猜你喜欢
    • 2018-01-22
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多