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

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

问题描述

我使用的WebForms和我试图执行一个母版页内部模型验证,由于某种原因该模型是不拿起值,这意味着,经过验证的火灾,如果我进入一个良好的价值,模型将保持未来空空的回来因此遍地触发验证。如果我把code页面没有母版页,它工作正常。我能够把一个很简单的例子,说实话,很难相信像普通MasterPages是,没有人碰到了这种情况为止。对于这个例子的目的,我嵌在code后面的型号,但有外不做任何不同。任何想法将大大欢迎。谢谢你。

- 主页 -

 <%@主语言=C#AutoEventWireup =真codeFILE =test.master.cs继承=测试%GT;<!DOCTYPE HTML>< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
    < ASP:的ContentPlaceHolder ID =头=服务器>
    < / ASP:&的ContentPlaceHolder GT;
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < ASP:的ContentPlaceHolder ID =ContentPlaceHolder1=服务器>        < / ASP:&的ContentPlaceHolder GT;
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

- Web窗体 -

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/ test.masterAutoEventWireup =真codeFILE =test3.aspx。 CS继承=TEST3%GT;< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
    < D​​IV>
        < ASP:的ValidationSummary ID =valSummary=服务器/>
    < / DIV>
        < D​​IV><标签>名字:< /标签>< / DIV>
        < D​​IV><输入类型=文本ID =名字=服务器/>< / DIV>
        < D​​IV><标签>中间名:< /标签>< / DIV>
        < D​​IV><输入类型=文本ID =中间名=服务器/>< / DIV>
        < D​​IV><标签>姓氏:其中; /标签>< / DIV>
        < D​​IV><输入类型=文本ID =姓氏=服务器/>< / DIV>
    < D​​IV>
        < ASP:按钮的ID =btnSubmit按钮=服务器文本=提交/>
    < / DIV>
< / ASP:内容>

- code的背后 -

 使用系统;
使用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
    {
        [需要()]
        [MINLENGTH个(2)]
        公共字符串名字{获得;组; }        公共字符串中间名{获得;组; }        [需要()]
        [MINLENGTH个(2)]
        公共字符串名字{获得;组; }
    }    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {        如果(Page.IsPostBack)
        {
            testClass2 TC =新testClass2();
            如果(TryUpdateModel(TC,新FormValueProvider(ModelBindingExecutionContext)))
            {
                System.Diagnostics.Debug.WriteLine(测试);
            }
        }
    }
}


解决方案

这个问题确实是母版页的使用造成的。当数据绑定控件之外使用(如 GridView控件 FormView控件菜单等), FormValueProvider 预计将在的Request.Form 字典作为模型对象属性名相同的密钥。

如果您需要在生成的HTML仔细看你会发现所有的输入标签的形式与母版页有名称设置为类似 ctl00 $ ContentPlaceHolder1 $姓 ctl00 $ ContentPlaceHolder1 $姓氏等,而无母版页的形式离开name属性不变,等于控制的 ID值属性。

这是怎样的 的UniqueID 生成,以避免重名(应该避免,因为在Web窗体,我们只能有一个形式标记,因此具有与对照相同的ID 这两个母版页和表单页)。

和这是原因为什么 FormValueProvider testClass2 对象无法获取值,它只是无法找到中间名值提交的表单。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆