ASP.NET MVC Html.Editor传递模型编辑器模板 [英] ASP.NET MVC Html.Editor pass model to Editor Template
问题描述
我有一个编辑模板被称为Address.cshtml已定义为模型:
I have an Editor Template called "Address.cshtml" that has a model defined as:
@model Acme.Models.Address
在一个视图我想调用编辑模板,并通过相同类型的局部变量,并将其定义将用于变量的名字,我已经尝试了一些事情,包括:
In a View I want call the Editor Template and pass a local variable of the same type, and define the name it will use for the variables, I've tried a number of things including:
@Html.Editor("address", "Address", new { Model = address })
我如何通过模型?
How do I pass the model?
请注意,我不能使用@ Html.EditorFor(),因为视图使用不同的模型。
Note, I cannot use @Html.EditorFor() because the view uses a different model.
推荐答案
EditorFor
的仅的目的是你的看法模型工作。如果你需要一个完全不同的类的实例,这不是你的视图模型或通过访问你视图的模型工作。然后,只需使用 Html.Partial
。他们是功能上是相同的。如果你担心使用特定的编辑模板,你可以随时通过完整路径以 Html.Partial
。
The only purpose of EditorFor
is to work with your view's model. If you need to work with a completely different class instance that's not your view's model or accessible through you're view's model. Then just use Html.Partial
. They're functionally the same. If you're worried about using a specific editor template, you can always pass the full path to the view to Html.Partial
.
这篇关于ASP.NET MVC Html.Editor传递模型编辑器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!