@Html.EditorForModel()
呈现~/Views/Shared/EditorTemplates/TypeName.cshtml
模板,其中TypeName
是视图模型的确切类型名称.如果您的视图模型是集合(即IEnumerable<TypeName>
,IList<TypeName>
,TypeName[]
,...),则ASP.NET MVC将自动为集合的每个元素呈现相应的编辑器模板.您无需在视图中编写任何循环即可实现这一点.它由框架为您处理.
Renders the ~/Views/Shared/EditorTemplates/TypeName.cshtml
template where TypeName
is the exact type name of your view model. If your view model is a collection (i.e. IEnumerable<TypeName>
, IList<TypeName>
, TypeName[]
, ...) ASP.NET MVC will automatically render the corresponding editor template for each element of the collection. You don't need to be writing any loops in your views for that to happen. It is handled by the framework for you.
@Html.EditorForModel("templatename")
渲染~/Views/Shared/EditorTemplates/templatename.cshtml
而不是依赖约定
@Html.EditorForModel(new { Foo = "bar" })
呈现默认的编辑器模板,但将其他视图数据传递给它,您可以在其中使用ViewData["foo"]
或ViewBag.Foo
Renders the default editor template but passes an additional view data to it that you could use inside with ViewData["foo"]
or ViewBag.Foo
@Html.EditorForModel("templatename", new { Foo = "bar" })
渲染~/Views/Shared/EditorTemplates/templatename.cshtml
而不是依赖于约定,并将其他视图数据传递给它,您可以将其与ViewData["foo"]
或ViewBag.Foo
Renders ~/Views/Shared/EditorTemplates/templatename.cshtml
instead of relying on the convention and passes an additional view data to it that you could use inside with ViewData["foo"]
or ViewBag.Foo
@Html.EditorForModel("templatename", "fieldprefix")
渲染~/Views/Shared/EditorTemplates/templatename.cshtml
而不是依赖于约定并修改此模板内的导航上下文,这意味着,例如,如果您在此模板内进行了@Html.TextBoxFor(x => x.FooBar)
调用,则会得到name="fieldprefix.FooBar"
而不是name="FooBar"
Renders ~/Views/Shared/EditorTemplates/templatename.cshtml
instead of relying on the convention and modifies the navigational context inside this template, meaning that for example if you had an @Html.TextBoxFor(x => x.FooBar)
call inside this template you would get name="fieldprefix.FooBar"
instead of name="FooBar"
@Html.EditorForModel("templatename", "fieldprefix", new { Foo = "bar" })
渲染~/Views/Shared/EditorTemplates/templatename.cshtml
而不是依赖于约定并修改此模板内的导航上下文,这意味着,例如,如果在此模板内进行了@Html.TextBoxFor(x => x.FooBar)
调用,则将得到name="fieldprefix.FooBar"
而不是name="FooBar"
.它还将附加的视图数据传递给它,您可以在ViewData["foo"]
或ViewBag.Foo
Renders ~/Views/Shared/EditorTemplates/templatename.cshtml
instead of relying on the convention and modifies the navigational context inside this template, meaning that for example if you had an @Html.TextBoxFor(x => x.FooBar)
call inside this template you would get name="fieldprefix.FooBar"
instead of name="FooBar"
. It also passes an additional view data to it that you could use inside with ViewData["foo"]
or ViewBag.Foo
备注:模板系统将首先在~/Views/XXX/EditorTemplates
中查找模板,其中XXX是为该视图提供服务的控制器的名称,如果找不到,它将进入~/Views/Shared/EditorTemplates
.这可以允许对模板进行更细粒度的调整.您可以在共享文件夹中有默认模板,可以按控制器将其覆盖.
Remark: The templating system will first look for templates in ~/Views/XXX/EditorTemplates
where XXX is the name of the controller that served this view and if it doesn't find it will look into ~/Views/Shared/EditorTemplates
. This could allow for more fine-grained tweaking of the templates. You could have default templates in the shared folder that could be overridden per controller basis.
这篇关于如何使用参数调用`EditorForModel`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!