服务器端脚本无法正常工作

时间:2013-04-02 16:30:14

标签: asp.net asp.net-mvc asp.net-mvc-3

enter image description here我是MVC新手并通过查看this视频创建示例应用程序。我在Model和控制器中创建了一个类。当我尝试通过视图访问Class属性时,我我无法访问它,即使我在视图中写作<%= Model.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; }

    }
}

和Controller是

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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您的观点使用Razor进行编码。

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

看看这里。

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