Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

c# - How should I call `EditorForModel` with its parameters?

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

I read Why not use Html.EditorForModel() and this blog.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are 6 overloads of this helper:

  1. @Html.EditorForModel()

    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.

  2. @Html.EditorForModel("templatename")

    Renders ~/Views/Shared/EditorTemplates/templatename.cshtml instead of relying on the convention

  3. @Html.EditorForModel(new { Foo = "bar" })

    Renders the default editor template but passes an additional view data to it that you could use inside with ViewData["foo"] or ViewBag.Foo

  4. @Html.EditorForModel("templatename", new { Foo = "bar" })

    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

  5. @Html.EditorForModel("templatename", "fieldprefix")

    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"

  6. @Html.EditorForModel("templatename", "fieldprefix", new { Foo = "bar" })

    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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...