views:

142

answers:

2

I want to use the updateModel in a controller that has no notice of the type of the view. I have different views that have different types but all have an ExternalBase class as inherited type.

So in my controller I always have a ExternalBase but the controller doesn't know the correct type.

On saving I call a method that gets the correct object but it returns this as an externalBase. The innertype is my correct type. If I hover over my object it is the type of the view that calls the save. Now if I call the updateModel on it it doesn't fill in the properties.

As an example:

// MyExternalBase is an empty class

The class person

public class Person 
  : MyExternalBase
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<TheParameters> Parameters { get; set; }
    public Address Address { get; set; }

    public TheParameters[] OtherParameters { get; set; }
}
public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public TheParameters Parameter { get; set; }
}
public class TheParameters
{
    public string Parameter { get; set; }
}

In my controller on the Save I do the following:

        MyExternalBase p = new Person();

        UpdateModel(p, "Person", form.ToValueProvider());

Now the p doesn't fill up.

If I instead use Person p = new Person() Then it is no problem. But I want my controller to be independent of the view type.

Is this an error in the updateModel or something that just isn't possible? Or is ther a workaround for it?

A: 

I too would like to know the answer to this, I have a very simular problem when using passing a base class even though I can safely downcast to the correct type with all the data in-tact.

UpdateModel doesn't seem to see the inner properties unless I downcast the basetype to the correct type, but this involves a switch statement to get the correct type.

Paul Hinett
+1  A: 

If you're using .NET 4.0, then try using:

dynamic p = new Person();
UpdateModel(p);

In that situation, UpdateModel will work properly. I ran into this as well when working on a single controller and view that I want to use for several objects that all inherit from the same type.

Brian Vallelunga
Totally what I needed! Thanks!
claco