What is @ in a view and controller?

8

I program in PHP and am starting to learn how to program in C # ASP.Net MVC, however I have some questions:

  • What is the @ (arroba) serving in both the controller and the view ?
  • What is the purpose of these { get; set; } calls that staff put into model ?

    public int id {get; set; } public string name {get; set; }

  • asked by anonymous 05.10.2015 / 18:06

    2 answers

    8
      

    What is the @ (arroba) serving in both the controller and the view ?

    In View , tells the Razor engine that the following code should be interpreted by .NET. Can be used on a line:

    @if (condicao) { ... }
    

    Or it can serve as opening a block:

    @{
        var minhaVariavel = ...;
    }
    

    In theory, the Razor engine interprets any .NET code, but the most used ones are C # and VB.NET.

    I do not recommend using the latter because business logic in View should be discouraged, since View is for presentation, not logic. It is important to know that the resource exists but that it should not be used as in PHP.

      

    What is the purpose of these { get; set; } calls that staff put into model ?

    They are automatic properties ( auto properties ). It's a concise way of writing the following:

    private int _id;
    public int id { 
        get { return _id; }
        set { _id = value; } 
    }
    

    Because property setting is widely used, leaner writing saves programmer effort.

        
    05.10.2015 / 18:16
    8

    In view using the rendering engine Razor it indicates that the following is a C # code and not HTML, so it will need to be executed and the result of this execution is that it will be rendered as part of HTML. See the entire Razor syntax . Use the minimum required.

    controller is a escape form .

    The public int id { get; set; } is a automatic property . This means that an internal member will be created in the class to save the state of id and there will be two methods to access its contents. This methods are called getter and setter . But they are not called as methods, the syntax is that as if you were accessing a variable. It's the same as:

    private int _id;
    public int id {
        get { 
          return _id; 
        }
        set {
          _id = value; 
        }
    }
    

    There are advantages and disadvantages of opting for a property . In model it is usually advantageous to indicate which members should save state.

        
    05.10.2015 / 18:11