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"); } } } }
user3679573
source share