ASP.NET 4.5 TryUpdateModel does not select form values ​​in WebForm using the master page

I use WebForms and I try to validate the model inside the main page, and for some reason the model does not pick up values, which means that after the validation works, if I enter a good value, the model continues to move back empty. which causes the test to be repeated again and again. If I put the code on a page without a master page, it works fine. I was able to give a very simple example and, frankly, it’s hard to believe that as often as MasterPages, so far no one has come across this scenario. For the purposes of this example, I have embedded the model in the "For" code, but the appearance is not different. Any idea would be very welcome. Thanks.

- master pages -

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="test.master.cs" Inherits="test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html> 

- WebForm -

 <%@ Page Title="" Language="C#" MasterPageFile="~/test.master" AutoEventWireup="true" CodeFile="test3.aspx.cs" Inherits="test3" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div> <asp:ValidationSummary ID="valSummary" runat="server" /> </div> <div><label>First Name:</label></div> <div><input type="text" id="FirstName" runat="server" /></div> <div><label>Middle Name:</label></div> <div><input type="text" id="MiddleName" runat="server" /></div> <div><label>Last Name:</label></div> <div><input type="text" id="LastName" runat="server" /></div> <div> <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> </div> </asp:Content> 

- Code for -

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.ModelBinding; using System.ComponentModel.DataAnnotations; public partial class test3 : System.Web.UI.Page { public class testClass2 { [Required()] [MinLength(2)] public string FirstName { get; set; } public string MiddleName { get; set; } [Required()] [MinLength(2)] public string LastName { get; set; } } protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { testClass2 tc = new testClass2(); if (TryUpdateModel(tc, new FormValueProvider(ModelBindingExecutionContext))) { System.Diagnostics.Debug.WriteLine("test"); } } } } 
+7
c # validation webforms master-pages
source share
2 answers

The problem is really caused by the use of the home page. When data binding control is used outside (for example, GridView , FormView , Menu , etc.), FormValueProvider expects to have the same keys in the Request.Form dictionary as property names in the model object.

If you familiarize yourself with the HTML code in more detail, you will see that all input tags in the form with the main page have a value for the name attribute set somehow like ctl00$ContentPlaceHolder1$FirstName , ctl00$ContentPlaceHolder1$LastName , etc., while a form without a main page leaves the name attribute intact and is equal to the value of the ID control property.

This is a way to create a UniqueID to avoid duplicate names (this should be avoided since in Web Forms we can only have one form tag and thus have controls with the same ID's on both the main page and the form page).

And this is why FormValueProvider cannot get values ​​for your testClass2 object, it just cannot find the FirstName , LastName , MiddleName in the published form.

+4
source share

I have found a solution. You can write a small front end code to set name to id and don't forget to set ClientIDMode in Static

 <script> $("input[type=text]").each(function () { var id = $(this).attr("id"); $(this).attr("name", id); }); </script> 
+1
source share

All Articles