In ASP.NET MVC, Generic [Try]UpdateModel methods ignore subtype properties - by Craig Stuntz

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


5
0
Sign in
to vote
ID 483001 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 8/14/2009 7:17:08 AM
Access Restriction Public

Description

Controller.TryUpdateModel<TModel> (and hence UpdateModel) uses typeof(TModel) instead of model.GetType() to determine the model type. I see almost no benefit to this. As a result, compile-time rather than runtime type knowledge is used, even though the properties will be bound via reflection anyway!

The fix would be to change the two cases of typeof(TModel) in TryUpdateModel to a call to model.GetType(). Note that model is required to be non-null anyway.

The workaround is to re-implement TryUpdateModel in your own code (ugh!) or call it via reflection (ugh!).
Sign in to post a comment.
Posted by Wout on 6/21/2011 at 1:00 PM
One more vote from me. In addition it would be nice if the UpdateModel/TryUpdateModel were public instead of protected. In case of runtime flexible binding some of the logic might be outside the Controller class.
Posted by Craig Stuntz on 8/20/2009 at 6:49 PM
You can only specify the type if you know it at compile time. If you don't know the tupe until runtime, you're left with two ugly options: Call TryUpdateModel via reflection or rewrite it. I wish there was at least the option of specifying a type at runtime, e.g. via an argument instead of a type parameter. There could be a different overload for this, though there are a lot already.
Posted by Microsoft on 8/20/2009 at 2:42 PM
Hi,

Thank you for reporting this issue. This is a deliberate design decision with the model binding feature of ASP.NET MVC. To override the type that gets used you can specify an explicit type when you call UpdateModel and TryUpdateModel:
    UpdateModel<DerivedType>(model);
When you leave out the type the compiler will use its type inference logic to guess the type that you meant. In this case it makes a guess that is incorrect.

Thanks,
The ASP.NET Team
Posted by Microsoft on 8/20/2009 at 1:31 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Thank you

Posted by Craig Stuntz on 8/18/2009 at 6:26 AM
I've uploaded a unit test project. You'll need VS 2008 and MVC 2 preview 1 to run it, although other versions *might* work. Just run the tests to see a demo of the bug and my proposed fix.
Posted by Craig Stuntz on 8/18/2009 at 6:25 AM
I've uploaded a unit test project. You'll need VS 2008 and MVC 2 preview 1 to run it, although other versions *might* work. Just run the tests to see a demo of the bug and my proposed fix.
Posted by Microsoft on 8/18/2009 at 12:57 AM
Thanks for reporting this issue. In order to fix the issue, we must first reproduce the issue in our labs. We are unable to reproduce the issue with the steps you provided.

Please give us a demo project to demonstrate this issue so that we can conduct further research? It is very important for us to resolve the issue more efficiently. Thanks for your cooperation and patience.

Please rest assured that it is for test purpose only. I have created a Workspace for you. Please upload the project file to it. Thanks for your time.

https://sftus.one.microsoft.com/choosetransfer.aspx?key=f635f8eb-1684-4608-aa9a-6a79b53d2fa7
Password is PoK$X6tPlLH0J]
Posted by Microsoft on 8/17/2009 at 2:13 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com/)