Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 - Razor - Print value from model

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

like image 608
G-Man Avatar asked Aug 23 '11 02:08

G-Man


People also ask

How do you display model data in Cshtml?

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".

Can you use MVC with Razor pages?

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.

How do you use a variable in razor view?

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.


1 Answers

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>
like image 69
Eranga Avatar answered Oct 10 '22 15:10

Eranga