I have some very basic code to simulate a form post into a controller that updates the model. In this case, say the "ActiveYesNo" was accidentially entered (Should be "ActiveYn" to match what's in the form).
SaveChanges runs ok but nothing is updated as expected. But I guess I thought there would be some type of binding error since there is no property called "ActiveYesNo" in the user object.
User user = new User()
var fakeForm = new FormCollection();
fakeForm.Add("ActiveYn", "true");
if (TryUpdateModel(user, new[] { "ActiveYesNo"}, fakeForm.ToValueProvider()))
{
db.SaveChanges();
}
else
{
throw new System.Exception("operation was invalid.");
}
The model binder doesn't produce any errors if it can't bind a property on a model object or if there are extra form values submitted with the page. The reason for this is because your model may include properties that aren't specific to this page, so they
are ignored. Perhaps your model has properties that aren't on the form, but need to be populated server side. For example a CreateDate field, it makes no sense to have this on the page and to model bind. Instead you would apply a value before saving to the
database, either in the controller or business layer.
In terms of the extra fields on the form, it could be the case that you have extra fields on the form that don't bind to a model. Either fields you will manuallly extract or it could be a case where a 3rd party is posting to your site (like after a paypal
success) and you only care about certain fields for your models and want to ignore the rest.
Both cases are not errors and as such don't produce an error.
The model binder only producers errors if a binding or validation rule fails. A binding rule would be something like a failure to convert an object to a specific type. For example an int property where someone entered a string would throw a binding error.
Or a blank entry binded to a int would fail because an int is non-nullable and there has to be a value.
Validation errors fire because of either data annonation validations or custom error validations. Now you can write a custom validation rule that checks all form inputs and throws an error if there is no corresponding property, but by default that behavior
isn't there for the reasons outlined above.
Dave5574
Member
30 Points
62 Posts
Trying to understand TryUpdateModel
Mar 27, 2012 08:23 PM|LINK
Hi,
I have some very basic code to simulate a form post into a controller that updates the model. In this case, say the "ActiveYesNo" was accidentially entered (Should be "ActiveYn" to match what's in the form).
SaveChanges runs ok but nothing is updated as expected. But I guess I thought there would be some type of binding error since there is no property called "ActiveYesNo" in the user object.
User user = new User()
var fakeForm = new FormCollection();
fakeForm.Add("ActiveYn", "true");
if (TryUpdateModel(user, new[] { "ActiveYesNo"}, fakeForm.ToValueProvider()))
{
db.SaveChanges();
}
else
{
throw new System.Exception("operation was invalid.");
}
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Trying to understand TryUpdateModel
Mar 27, 2012 08:39 PM|LINK
The model binder doesn't produce any errors if it can't bind a property on a model object or if there are extra form values submitted with the page. The reason for this is because your model may include properties that aren't specific to this page, so they are ignored. Perhaps your model has properties that aren't on the form, but need to be populated server side. For example a CreateDate field, it makes no sense to have this on the page and to model bind. Instead you would apply a value before saving to the database, either in the controller or business layer.
In terms of the extra fields on the form, it could be the case that you have extra fields on the form that don't bind to a model. Either fields you will manuallly extract or it could be a case where a 3rd party is posting to your site (like after a paypal success) and you only care about certain fields for your models and want to ignore the rest.
Both cases are not errors and as such don't produce an error.
The model binder only producers errors if a binding or validation rule fails. A binding rule would be something like a failure to convert an object to a specific type. For example an int property where someone entered a string would throw a binding error. Or a blank entry binded to a int would fail because an int is non-nullable and there has to be a value.
Validation errors fire because of either data annonation validations or custom error validations. Now you can write a custom validation rule that checks all form inputs and throws an error if there is no corresponding property, but by default that behavior isn't there for the reasons outlined above.
Blog | Twitter : @Hattan