ASP.NET 4.5 TryUpdateModel 不使用母版页在 WebForm 中选择表单值 [英] ASP.NET 4.5 TryUpdateModel not picking Form values in WebForm using Master-Page

查看:26
本文介绍了ASP.NET 4.5 TryUpdateModel 不使用母版页在 WebForm 中选择表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebForms 并且我正在尝试在母版页中执行模型验证,但由于某种原因,模型没有获取值,这意味着如果我输入一个好的值,验证触发后,模型会继续出现返回空,从而一遍又一遍地触发验证.如果我将代码放在没有母版页的页面中,它可以正常工作.我可以举一个非常简单的例子,老实说,很难相信像 MasterPages 一样普遍,到目前为止还没有人遇到过这种情况.出于本示例的目的,我将模型嵌入到背后的代码中,但外部没有什么不同.任何想法都会受到极大欢迎.谢谢.

--母版页--

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

</表单>

-- 网页表单 --

<%@ Page Title="" Language="C#" MasterPageFile="~/test.master" AutoEventWireup="true" CodeFile="test3.aspx.cs" Inherits="test3" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:内容><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="服务器"><div><asp:ValidationSummary ID="valSummary" runat="server"/>

<div><label>名字:</label></div><div><input type="text" id="FirstName" runat="server"/></div><div><label>中间名:</label></div><div><input type="text" id="MiddleName" runat="server"/></div><div><label>姓氏:</label></div><div><input type="text" id="LastName" runat="server"/></div><div><asp:Button ID="btnSubmit" runat="server" Text="Submit"/>

</asp:内容>

-- 背后的代码 --

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;使用 System.Web.ModelBinding;使用 System.ComponentModel.DataAnnotations;公共部分类 test3 : System.Web.UI.Page{公开课 testClass2{[必需的()][最小长度(2)]公共字符串名字{获取;放;}公共字符串 MiddleName { 获取;放;}[必需的()][最小长度(2)]公共字符串姓氏 { 获取;放;}}protected void Page_Load(object sender, EventArgs e){如果 (Page.IsPostBack){testClass2 tc = 新的 testClass2();if (TryUpdateModel(tc, new FormValueProvider(ModelBindingExecutionContext))){System.Diagnostics.Debug.WriteLine("test");}}}}

解决方案

问题确实是使用母版页引起的.在数据绑定控件之外使用时(如GridViewFormViewMenu等),FormValueProvider期望有相同Request.Form 字典中的键作为模型对象中的属性名称.

如果您仔细查看生成的 HTML,您将看到所有 input 标记在母版页的表单中都将 name 属性值设置为类似 ctl00$ContentPlaceHolder1$FirstNamectl00$ContentPlaceHolder1$LastName 等,而没有母版页的表单保持 name 属性不变并等于控件的 ID 属性的值.

这是UniqueID 是为了避免名称重复(应该避免,因为在 Web Forms 中我们只能有一个 form 标签,因此有在母版页和表单页上使用相同的 ID 控件).

这就是为什么 FormValueProvider 无法为您的 testClass2 对象获取值的原因,它只是找不到 FirstName, LastName, MiddleName 以发布形式显示的值.

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 validation fires if I enter a good value, the model keeps coming back empty hence triggering the validation over and over. If I put the code in page without Master-Page it works fine. I was able to put a very simple example and honestly, it's hard to believe that as common as MasterPages are, nobody has ran into this scenario so far. For the purpose of this example I embedded the Model in the Code Behind but having outside makes no different. Any idea will be greatly welcome. Thanks.

--Master-Page--

<%@ 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 Behind --

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

解决方案

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.

这篇关于ASP.NET 4.5 TryUpdateModel 不使用母版页在 WebForm 中选择表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆