c# - ASP.NET C# MVC 将值从 Controller 传递到 cshtml

标签 c# asp.net asp.net-mvc razor

我刚开始使用 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/

相关文章:

c# - 如何使用C#知道Excel中的分页符

c# - ASP.NET 中的 ClientIDMode ="AutoID"

c# - 远程服务器返回错误 : (412) The condition specified using HTTP conditional header(s) is not met. 。

asp.net-mvc - Bootstrap 响应式侧菜单

c# - 如何从 Excel 单元格显示值读取而不用 ClosedXML 计算公式

c# - 请求在 Helper 方法上被中止 : The connection was closed unexpectedly.

c# - Skype for Business 2016 没有客户端 SDK

c# - 'propertyName' 不能用作实体类型 'typeName' 的属性,因为它被配置为导航

c# - 如何确定 IResourceFilter 中的 Controller 名称和 Action 名称?

第二个 API 上的 ASP.Net Web API 404 错误