I am trying to set the value of a label using Razor, I have a model and
<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 ?
Thank you
ViewData Model Thereafter create a view ("StudentInformation. cshtml") that shows the data of the student object data. ViewData has a Model property that has this object when it is passed from the controller so you can get the Student object from "ViewData. Model".
You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.
To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.
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.
<label id="status">
@{
if (Model.Count() > 0)
{
@Model.First().StatusName.ToString()
}
}
</label>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With