如何使用参数调用`EditorForModel`? [英] How should I call `EditorForModel` with its parameters?

查看:138
本文介绍了如何使用参数调用`EditorForModel`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布此问题之前,我使用参数在Google搜索了EditorForModel.

Before posting this question, I googled for EditorForModel using parameters.

我阅读了为什么不使用Html.EditorForModel()和此博客.

我没有找到任何与我的需求有关的文章.

I didn't find any articles related to my needs.

您能提供给我一个使用参数调用EditorForModel的示例吗?

Can you provide me an example of a call to EditorForModel with parameters?

推荐答案

  • @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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆