简体   繁体   中英

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. 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. It sets the id of the textbox which is obviously important.

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?

Maybe use ViewData.TemplateInfo.HtmlFieldPrefix instead of 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.

You can modify the HtmlFieldPrefix property with the htmlFieldName parameter from Html.Editor.

You can use the UIHint in your model. give it the name of the template you want to use

[UIHint("DateWithTime")]

You still use EditorFor with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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