I am using WebForms and I am trying to perform Model Validation inside a Master-Page and for some reason the model is not picking up the values, meaning that after the validatio
I found a solution.You can write a little front-end code to set the name
equal to id
and don't forget to set ClientIDMode
to Static
<script>
$("input[type=text]").each(function () {
var id = $(this).attr("id");
$(this).attr("name", id);
});
</script>
The problem is indeed caused by usage of the master page. When used outside of the data bound control (such as GridView
, FormView
, Menu
, etc.), FormValueProvider
expects to have the same keys in Request.Form
dictionary as property names in model object.
If you will take a closer look at generated HTML you will see that all input
tags in form with master page have name
attribute value set to something like ctl00$ContentPlaceHolder1$FirstName
, ctl00$ContentPlaceHolder1$LastName
, etc. while form without master page leaves name attribute intact and equal to the value of control's ID
property.
This is the way of how UniqueID is generated to avoid name duplication (which should be avoided because in Web Forms we can have only one form
tag and thus have controls with same ID's
both on master page and form page).
And this is the cause why FormValueProvider
cannot get values for your testClass2
object, it just cannot find FirstName
, LastName
, MiddleName
values in posted form.