该视图显示<%= Id%>而不是实际值

时间:2016-05-20 05:44:42

标签: asp.net-mvc

我是ASP.NET MVC的新手,正在尝试在视图中显示数据,但会显示说明而不是数据。

我的CustomerModel类;

public class CustomerModel
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Nic { get; set; }
}

以下是Customercontroller

public class CustomerController : Controller
{
    public ActionResult Index(CustomerModel Customerobj)
    {
        Customerobj.Id = 1;
        Customerobj.FirstName = "Qaiser";
        Customerobj.LastName = "Abbas";
        Customerobj.Nic = "61101";
        return View(Customerobj);
    }
}

客户视图

@model SmartMinds.Models.CustomerModel
<div>
    The Id of Customer is : <%=Id%>
    The First Name of Customer is:<%CustomerModel.FirstName%>
    The Last Name of Customer is:<%CustomerModel.LastName%>
    The  Nic of Customer is:<%CustomerModel.Nic%>
</div>

OutPut

The Id of Customer is : <%=Id%>
The First Name of Customer is:<%CustomerModel.FirstName%>
The Last Name of Customer is:<%CustomerModel.LastName%> 
The Nic of Customer is:<%CustomerModel.Nic%>

它没有显示真实值

2 个答案:

答案 0 :(得分:1)

您只需要在视图发送数据时(或者您希望从查询字符串中获取信息时)提供操作参数,而不是在您只想显示数据时。

public ActionResult Index()
{
    CustomerModel Customerobj = new CustomerModel();
    Customerobj.Id = 1;
    Customerobj.FirstName = "Qaiser";
    Customerobj.LastName = "Abbas";
    Customerobj.Nic = "61101";

    return View(Customerobj);
}

在视图中,您需要使用razor语法:

<div>
    The Id of Customer is : @Model.Id
    The First Name of Customer is: @Model.FirstName
    The Last Name of Customer is: @Model.LastName
    The  Nic of Customer is: @Model.Nic
</div>

答案 1 :(得分:0)

使用剃刀视图时,您需要使用@并使用@Model获取模型

<div>

The Id of Customer is : @Model.Id

The First Name of Customer is:@Model.FirstName

The Last Name of Customer is:@Model.LastName

The  Nic of Customer is:@Model.Nic

</div>