mvc uppercase Model vs lowercase model

后端 未结 1 730
渐次进展
渐次进展 2020-11-27 07:19

I\'m working on a MVC 5 project, very new to MVC. I noticed this line in the code:

@Html.DropDownListFor(model => model.ContractorId, Model.Contractors)
         


        
相关标签:
1条回答
  • 2020-11-27 07:58

    Model actually represents an instance of the class that is your model and model is an alias for lambda expression.

    When you write @Model in your view you are using the object of ContractViewModel which is passed from Controller action, if it is not passed from View it can be null and accessing any property of Model can throw Null Reference Exception and writing Model.Contractors you basically mean ContractViewModel.Contractors

    and when you write a html helper you need to write an alias for it, its not mandatory to write model you can write anything.

    for example:

    @Html.DropDownListFor(m=> m.ContractorId, Model.Contractors)
    

    It is just an alias to access the model properties inside Html Helper.

    When you write @model Project.Models.ContractViewModel at the top of View that is a different thing, in this case we are defining the model of view that it will take instance of Project.Models.ContractViewModel , actually our view is now strongly typed to instance of class Project.Models.ContractViewModel.

    0 讨论(0)
提交回复
热议问题