代码之家  ›  专栏  ›  技术社区  ›  F11

服务器端脚本不起作用

  •  -1
  • F11  · 技术社区  · 13 年前

    enter image description here 我是MVC的新手,通过查看 this 视频。我已经在Model中创建了一个类和一个控制器。现在,当我试图通过视图访问class属性时,我无法访问它,即使我在视图中写作,如<%=型号Id%>,角括号没有像服务器端脚本那样标记为黄色。我找不到问题出在哪里?

    模型中的类别客户

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcCustomer.Models
    {
        public class Customer
        {
            public int Id { get; set; }
            public string  Name { get; set; }
            public double Amount { get; set; }
    
        }
    }
    

    控制器是

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcCustomer.Models;
    
    namespace MvcCustomer.Controllers
    {
        public class LoadCustomerAndDisplayController : Controller
        {
            //
            // GET: /LoadCustomerAndDisplay/
    
            public ActionResult Index()
            {
                Customer customer = new Customer();
                customer.Name = "Devesh";
                customer.Amount = 22.9;
                customer.Id = 1;
    
                return View(customer);
            }
    
        }
    }
    

    而我的强类型视图(IntelliSense也不起作用)是

    @model MvcCustomer.Models.Customer
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <title>Index</title>
    </head>
    <body>
        <div>
            The Customer id is: <%= Model.Id %>
        </div>
    </body>
    </html>
    
    2 回复  |  直到 13 年前
        1
  •  1
  •   Chris Pratt    13 年前

    首先,您使用的是Razor视图,而不是ASP.NET视图,因此语法为 @Model.Id <%= Model.Id %> .

        2
  •  0
  •   Daniel A. White    13 年前

    您的视图是用Razor编码的。

    要启动服务器端行为,请使用 @ <%= ... %>

    看看这里。

    http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx