I'm using custom editor templates for most of my forms, but while attempting to use them to render a custom object picker I've found no easy way to pass information about the containing context to the editor template.
Specifically, my main form renders an edit for a domain object, and the editor template needs to render an AJAX picker which contains a list of objects which are dependent on the ID of the domain object. I'm passing the ID using the additionalViewData
parameter currently, which I think is error prone and therefore quite ugly.
My form contains code similar to the following:
@Html.EditorFor(model => model.CategoryId, new { id = model.id })
The editor template contains code like the following:
@{
var domainObjectId = ViewData["id"] as int?;
}
I'm using a custom ModelMetadataProvider
to select the object picker editor template, and hope to use a similar technique to pass information about the containing model to the editor template, but that doesn't seem be possible.
So, my questions are:
- Is there anyway to use a
ModelMetadataProvider
to pass information about the containing model to the editor template? - If not, is there any neater/easier way to achieve what I'm attempting, aside from passing every piece of additional information via the weakly typed
additionalViewData
parameter?
Thanks in advance!