ASP.NET MVC - Pass outer model to editor template

2019-06-25 04:06发布

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:

  1. Is there anyway to use a ModelMetadataProvider to pass information about the containing model to the editor template?
  2. 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!

2条回答
手持菜刀,她持情操
2楼-- · 2019-06-25 04:23

You need to understand that EditorTemplates are designed to be type specific, not context specific. Some thought was given to this with the AdditionalViewData parameter, but that's the best you're going to get.

If you're concerned about type safety, use ViewBag instead, which is a type-safe dynamic wrapper around ViewData.

@{
    var domainObjectId = ViewBag.Id;
}
查看更多
可以哭但决不认输i
3楼-- · 2019-06-25 04:34

I wonder if maybe the controller that is creating this view model in the first place should be the thing that chooses the list of objects. So rather than having an integer property on your view model, you could have a sub property of another type of view model, ie:

public class OuterViewModel
{
    public CategoryViewModel Category { get; set; }
}

public class CategoryViewModel
{
    public int CategoryId { get; set; }

    public IEnumerable<SelectListItem> ListOfThings { get; set; }
}

Then your original view can just have:

@Html.EditorFor(model => model.Category)

With an EditorTemplate for CategoryViewModel that looks like:

@model CategoryViewModel
@Html.DropDownFor(m => m.CategoryId, Model.ListOfThings)

The only thing you have to remember this is if you do any server side business logic validation, add model errors and return to your view you will need to re populate your list of things in your controller post action.

查看更多
登录 后发表回答