Item 225 of 295 Previous | Next

8
Vote

Add System.Web.Mvc.ModelMetadata.Container property

description

It would be great if there will be property Container on ModelMetadata class that will hold current container instance.
Why? It would help to implement generic DropdownListing using templates (http://weblogs.asp.net/rashid/archive/2009/11/27/extending-asp-net-mvc-2-templates.aspx).

No files are attached

comments

TerminalFrost wrote Sep 18 2011 at 2:28 AM

It's too bad that this issue hasn't been address in MVC3. Augi and Wayne point out exactly the issue that I ran into. Using custom editor templates is a great feature of MVC but the functionality is severely limited by not providing access to the model's container instance. Are there any plans to address this issue?

WayneWong wrote Apr 21 2010 at 7:33 AM

I met with the issue also. The container instance is needed to write some template like dropdown list, checkbox list or radiobox list.

Augi wrote Jan 8 2010 at 11:39 AM

I would like to use this code:
public class MyViewModel
{
[DropdownList("Categories")] // refers to another property that contains all categories
public Category Category { get; set; }

public IEnumerable<Category> Categories { get; set; }
}

Then I must access Categories property of MyViewModel class but this is not available in Template for Category property.

kazimanzurrashid wrote Jan 8 2010 at 11:16 AM

Currently the only workaround is to use the string based expression of ViewData as it replace the model in the child.

kazimanzurrashid wrote Jan 8 2010 at 11:13 AM

Yes try this:

public class MyModel
{
public IList<Category> Categories
{
get; set;
}

public Category SelectedCategory
{
get; set;
}
}

Now I have a template for Category which will be shown as DropDownList with the above Categories. So in the Category we need to access the parent (MyModel) to populate the dropdown.

BradWilson wrote Jan 8 2010 at 12:01 AM

Can you give a use case for accessing the parent model from a template of a child?

kazimanzurrashid wrote Dec 25 2009 at 8:48 AM

I think Augi is absolutely right, otherwise it is not possible to access the parent model from the template.