2

What is the right way to use (Try)UpdateModel?

When I run this:

  • TryUpdateModel returns true,
  • ViewData has no errors,
  • but my Proxy is not updated.

Action Method

public void Save(string TypeName, int Id, FormCollection idontknow) {
    var types = Assembly.GetExecutingAssembly().GetTypes();
    var ObjectType=(from t in types where t.Name == TypeName select t).First();
    var Proxy = context.Set(ObjectType).Find(Id); // EF 4.1
    if (TryUpdateModel(Proxy, TypeName)) {
        var x = ViewData.GetModelStateErrors(); // no errors
    }
}

Posted Data

TypeName=Thing&Id=1&Thing.Id=1&Thing.Name=hello&Thing.OptionID=2

Thing Class

public class Thing : Base {
    public virtual Nullable<int> OptionID { get; set; }
    public virtual Option Option { get; set; }
    public virtual ICollection<ListItem> ListItems { get; set; }
}
public class Base {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    [NotMapped]
    public virtual int? EntityState { get; set; }
}

EDIT: I also tried passing the form collection explicitly TryUpdateModel(Proxy, TypeName, idontknow)

EDIT #2: (in response to NickLarsen)

  1. Restarted VS and server, no change.
  2. Values are actually in the FormCollection.
  3. Mock data works! I know I must be messing up something here.
  4. Using debugger to check values.
Benjamin
  • 3,134
  • 6
  • 36
  • 57

1 Answers1

1

I stripped all the EF stuff and tried to get just that query string to populate the model with the values... and it worked just fine.

//controller class
public ActionResult Save(string TypeName, int Id, FormCollection idontknow)
{
    var Proxy = new Thing
    {
        Id = 33,
        OptionID = 2234,
        Name = "tony",
    };
    if (TryUpdateModel(Proxy, TypeName))
    {
        ViewBag.Message = "WInner";
    }

    return RedirectToAction("Index");
}
//end controller class

public class Thing : Base
{
    public virtual Nullable<int> OptionID { get; set; }
}
public class Base
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

Honestly I can't figure think of what in your code would keep it from working, but I would suggest going through the list 1 by one and testing after each step...

  1. Save your progress and restart VS and your development server
  2. Check that the values are actually in the form data, maybe something is getting in the way there.
  3. Mock up some trash data like I did. (checking if the problem has something to do with EF)
  4. How are you identifying that Proxy isn't being updated? In the debugger, on the page, etc?
  5. Edit your question with the answer to all of the above questions.
Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
  • Thanks for the reply. I updated the question. Maybe I should start with a fresh test project..? – Benjamin Jul 29 '11 at 03:44
  • I don't really understand proxies yet. Does it fail because of the proxy type != "Thing"? `System.Data.Entity.DynamicProxies.Thing_38966D698AE5B953EEFA4FC1D5DC769DAA3F707D357584ACF00CBBD2254BECB1` – Benjamin Jul 29 '11 at 03:52
  • @Benjamin: That should not matter at all, the TypeName is just specifying the prefix for the values to match against the attributes of the model passed in, doesn't matter the type of the model, just the properties it has. – Nick Larsen Jul 29 '11 at 15:02
  • Ok, that's good. It's weird it doesn't work then. I am looking at an alternative means to the same end, using a custom model binder [link](http://stackoverflow.com/questions/6861683/how-can-i-make-a-controller-action-take-a-dynamic-parameter) – Benjamin Jul 29 '11 at 20:55