What is difference between Model and @Model?

Coreysan 1,811 Reputation points
2022-11-10T20:44:02.107+00:00

I was learning to test a string being passed to a view,
and ended up doing it this way:

@model string  
  
@if (Model != null)  
{  
    <div>  
        <p>  
            User is not registered. Please register first.  
        </p>  
    </div>  
}  

But when I was testing it, I ignorantly but a "@" character in front of Model like this:

@model string  

@if (@Model != null)  
{  
    <div>  
        <p>  
            User is not registered. Please register first.  
        </p>  
    </div>  
}  

They both work, but I don't understand why. Obviously the character "@"
has meaning I don't fully understand.

How can I best understand this?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-11-11T02:49:34.06+00:00

    Hi @Coreysan ,

    In MVC Razor view, the @model directive allows access to the object that the controller passed to the view. The Model object is strongly typed, it will get transferred model from the ViewData.

    So, in your scenario, if you set break point it check Model in the @if condition, you can see no matter using Model or @Model, they all get the strongly typed model. But inside the if statement, if you are using the Model or @Model, you can see the difference: use the @Model, it will display the value.

    259363-image.png

    More detail about the Razor syntax, see Razor syntax reference for ASP.NET Core and Strongly typed models and the [@](/users/na/?userId=0f55de7e-bffd-0003-0000-000000000000) directive


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Coreysan 1,811 Reputation points
    2022-11-11T20:51:59.423+00:00

    Thanks so much!

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-11-11T23:33:12.277+00:00

    the @ character has a meaning in razor syntax and author in c#. in razor you exit razor mode to c# mode with the @ character. also razor directives start with an @ sign:

    @默 ModelType

    <div>this is razor, start c# @默 .SomeProp back to razor</div>
    @if // start inline c#

    in c# the @ is can used as a prefix to variables if they are reserved works (they don't have to be)

    var @return = ""; // must always use @return because return is a reserved word
    var @s = ""; // can use s or @s when referencing the variable because it is not a reserved word.

    so in your code:

    @if (@默 != null)

    the first @ enters c# mode. the @默 , the @ is a c# variable prefix (unnecessary but legal syntax).

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.