asp.net-mvc MVC 3 - Razor - 从模型打印值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7155640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC 3 - Razor - Print value from model
提问by G-Man
I am trying to set the value of a label using Razor, I have a model and
我正在尝试使用 Razor 设置标签的值,我有一个模型和
<label id="status">
@{
if (Model.Count() > 0)
{
Model.First().StatusName.ToString();
}
}
</label>
If I put a breakpoint on Model.First().StatusName.ToString(); I can see that that expression has the value that I need, but I cannot see it when the page gets rendered - Am I missing something in my syntax ?
如果我在 Model.First().StatusName.ToString(); 上放置一个断点;我可以看到该表达式具有我需要的值,但是在页面呈现时我看不到它 - 我是否在语法中遗漏了什么?
Thank you
谢谢
回答by Eranga
You need to add @sign before Model.First().StatusName.ToString()to let Razor know that you are outputting something. Otherwise it will treat it as ordinary method call.
你需要在@之前添加符号Model.First().StatusName.ToString()让 Razor 知道你正在输出一些东西。否则它会将其视为普通的方法调用。
<label id="status">
@{
if (Model.Count() > 0)
{
@Model.First().StatusName.ToString()
}
}
</label>

