简体   繁体   English

使用编辑模板而不使用Html.EditorFor()

[英]Using a edit template without using Html.EditorFor()

I have a date time picker combination in a edit template that can be used like Html.EditorFor(x => x.ETA) but now I want to use the same template somewhere where I don't have a model that contains a DateTime property. 我在编辑模板中有一个日期时间选择器组合,可以像Html.EditorFor(x => x.ETA)这样使用,但是现在我想在没有模型包含DateTime属性的地方使用同一模板。 So I tried Html.Editor("DateWithTime", "Arrival") which uses the correct template, but doesn't assign a value to ViewData.ModelMetadata.PropertyName which is something that my template relies on. 所以我尝试了Html.Editor(“ DateWithTime”,“ Arrival”),它使用了正确的模板,但是没有为ViewData.ModelMetadata.PropertyName分配值,这是我的模板所依赖的。 It sets the id of the textbox which is obviously important. 它设置了文本框的ID,这显然很重要。

Is there a way to render the template and assign a id value to the ViewData.ModelMetadata.PropertyName so I can re-use the logic in the template instead of having to copy it? 有没有一种方法可以渲染模板并将ID值分配给ViewData.ModelMetadata.PropertyName,以便我可以重用模板中的逻辑,而不必复制它?

Maybe use ViewData.TemplateInfo.HtmlFieldPrefix instead of ViewData.ModelMetadata.PropertyName. 也许使用ViewData.TemplateInfo.HtmlFieldPrefix代替ViewData.ModelMetadata.PropertyName。 I am not sure but I thought that HtmlFieldPrefix an PropertyName have the same value as long as you do not iterate a collection. 我不确定,但是我认为HtmlFieldPrefix和PropertyName具有相同的值,只要您不迭代集合即可。

You can modify the HtmlFieldPrefix property with the htmlFieldName parameter from Html.Editor. 您可以使用Html.Editor中的htmlFieldName参数修改HtmlFieldPrefix属性。

You can use the UIHint in your model. 您可以在模型中使用UIHint。 give it the name of the template you want to use 为它指定要使用的模板的名称

[UIHint("DateWithTime")]

You still use EditorFor with this. 您仍与此结合使用EditorFor。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM