ASP.NET 4.5 TryUpdateModel not picking Form values in WebForm using Master-Page

后端 未结 2 507
长情又很酷
长情又很酷 2021-02-15 07:03

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

相关标签:
2条回答
  • 2021-02-15 07:40

    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>
    
    0 讨论(0)
  • 2021-02-15 07:46

    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.

    0 讨论(0)
提交回复
热议问题