我刚开始使用 Razor 页面,所以我在模型目录中有一个具有以下值的类:
public int Id { set; get; }
public string CustomerCode { set; get; }
public double Amount { set; get; }
在我的 Controller (.cs 文件)中,我有以下内容:
public ActionResult Index()
{
Customer objCustomer = new Customer();
objCustomer.Id = 1001;
objCustomer.CustomerCode = "C001";
objCustomer.Amount = 900.78;
return View();
}
...现在,我想通过我的 Index.cshtml 页面显示这些值,但是当我运行该应用程序时,我只得到我输入的与这些值相反的实际代码:
...这是 .cshtml 页面设置的方式:
@model Mvccustomer.Models.Customer
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
The customer id is : <%= Model.Id %> <br />
The customer id is : <%= Model.CustomerCode %> <br />
The customer id is : <%= Model.Amount %> <br />
</div>
...我的问题是,如何获取要显示的值?在此先感谢您的帮助。
最佳答案
您需要使用 razor syntax .
使用你的例子:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
The customer id is : @Model.Id <br />
The customer id is : @Model.CustomerCode <br />
The customer id is : @Model.Amount <br />
</div>
关于c# - ASP.NET C# MVC 将值从 Controller 传递到 cshtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49415615/