MVC模式的大写小写VS模式 [英] mvc uppercase Model vs lowercase model
问题描述
我工作的一个MVC 5项目,MVC很新。我注意到,在code此行:
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)
页面顶部的指令是:
the directive on the top of the page is:
@model Project.Models.ContractViewModel
该ContractViewModel类确实有一个承包商在它的对象。
The ContractViewModel class does have a Contractors object in it.
public IEnumerable<SelectListItem> Contractors
我只是对资本M(Model.Contractors)有点糊涂,不模型总是指在@model传递的对象?然后使用Model.Contractors和model.Contractors之间的区别是什么呢?
I'm just a little confused on the capital M (Model.Contractors), does Model always refer to the object passed in the @model? Then what is the difference between using Model.Contractors and model.Contractors?
推荐答案
型号实际上重新presents这是您的模型类的一个实例并模型一个别名拉姆达前pression。
Model actually represents an instance of the class that is your model and model is an alias for lambda expression.
当你写 @Model
在你看来,你所使用的是从控制器操作传递 ContractViewModel
的对象,如果不从View通过它可以为空和访问模型的任何财产可以扔的空引用异常并写 Model.Contractors
基本上你意味着 ContractViewModel.Contractors
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
和当你写一个HTML帮手,你需要为它编写一个别名,它不是强制性的写模式
你可以写任何东西。
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.
例如:
@Html.DropDownListFor(m=> m.ContractorId, Model.Contractors)
这只是进入里面的模特属性别名 HTML辅助
当你写 @model Project.Models.ContractViewModel
在视图顶部这是一个不同的事情,在这种情况下,我们正在定义来看,它会模型采取 Project.Models.ContractViewModel
,其实我们认为现在是强类型的类实例 Project.Models.ContractViewModel
。
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
.
这篇关于MVC模式的大写小写VS模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!