使用Mvc客户端验证时,formElement为null

时间:2010-03-03 05:10:49

标签: javascript asp.net-mvc asp.net-mvc-validation

尝试使用MVC客户端验证时,我得到“formElement为null”。 有没有人对这个问题有什么想法?

Sys.Mvc.NumberValidator.create = function(rule){return Function.createDelegate(new Sys.Mvc.NumberValidator(),new Sys.Mvc.NumberValidator()。validate);}

这是我的模特:


public class EmailViewModel
    {
        /// 
        /// The user's current email address
        /// 
        public string CurrentEmailAddress { get; set; }

        /// 
        /// User's new email address
        /// 
        [EmailAddress( IsRequired = true, ErrorMessage = "Please enter a valid email address." )]
        public string NewEmailAddress { get; set; }

        /// 
        /// User's confirmed new email address
        /// 
        [EmailAddress( IsRequired = true, ErrorMessage = "Please enter a valid email address. Your emails do not match." )]
        public string ConfirmNewEmailAddress { get; set; }

        public EmailViewModel()
        {
            CurrentEmailAddress = "Michael.l.paterson@gmail.com";
            NewEmailAddress = string.Empty;
            ConfirmNewEmailAddress = string.Empty;
        }
    }

以下是母版页中的脚本引用:

MicrosoftAjax.js
MicrosoftMvcAjax.js
MicrosoftMvcValidation.js

我不确定是否所有这些都会显示,但这里是视图代码:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

<%= Html.ValidationSummary( true, "There was an error when processing your request" ) %>
<% Html.EnableClientValidation(); %>
<% Html.BeginForm(); %>
    <div class="SecureForm_Wrapper">
        <label class="Styled">
            <img alt="Reqired" src="../../Content/Images/Misc/required.png" />
            New E-Mail</label>
        <div class="TheField">
            <%= Html.TextBoxFor( m => m.NewEmailAddress, new { id = "NewEmailAddress", name = "NewEmailAddress", @class = "CommonTextBox" } ) %>
            <%= Html.ValidationMessageFor( m => m.NewEmailAddress ) %>
        </div>
    </div>
    <div class="SecureForm_Wrapper">
        <label class="Styled">
            <img alt="Reqired" src="../../Content/Images/Misc/required.png" />
            Confirm New E-Mail</label>
        <div class="TheField">
            <%= Html.TextBoxFor( m => m.ConfirmNewEmailAddress, new { id = "ConfirmNewEmailAddress", name = "ConfirmNewEmailAddress", @class = "CommonTextBox" } )%>
            <%= Html.ValidationMessageFor( m => m.ConfirmNewEmailAddress ) %>
        </div>
    </div>
    <div id="BottomButtonContainer">
        <a class="button" href="#" id="SubmitButton" style="font-weight: bold;"><span>Save</span></a>
        <a class="button" href="#" onclick="this.blur(); return false;"><span>Cancel</span>
        </a>
    </div>
    <% Html.EndForm(); %>

这是

有没有人对可能出现的问题有任何想法?

1 个答案:

答案 0 :(得分:0)

当我将视图模型更改为使用Required属性(并删除IsRequired属性)时,您的视图模型和视图似乎对我来说很好。我还必须在表单中添加一个提交按钮。也许问题在于您实施EmailAddress验证器?

您可能希望对问题的格式更加小心,因为它使解析起来有点困难。作为参考,这里是我正在使用的视图模型和清理视图。

查看型号:

public class EmailViewModel
{
    /// 
    /// The user's current email address
    /// 
    public string CurrentEmailAddress { get; set; }

    /// 
    /// User's new email address
    /// 
    [Required(ErrorMessage = "Please enter a valid email address.")]
    public string NewEmailAddress { get; set; }

    /// 
    /// User's confirmed new email address
    /// 
    [Required(ErrorMessage = "Please enter a valid email address. Your emails do not match.")]
    public string ConfirmNewEmailAddress { get; set; }

    public EmailViewModel()
    {
        CurrentEmailAddress = "Michael.l.paterson@gmail.com";
        NewEmailAddress = string.Empty;
        ConfirmNewEmailAddress = string.Empty;
    }
}

查看:

<%= Html.ValidationSummary( true, "There was an error when processing your request" ) %>
<% Html.EnableClientValidation(); %>

<% using (Html.BeginForm())
   { %> 

    New E-Mail 
    <%= Html.TextBoxFor(m => m.NewEmailAddress)%> 
    <%= Html.ValidationMessageFor(m => m.NewEmailAddress)%> 
    <br />

    Confirm New E-Mail 
    <%= Html.TextBoxFor(m => m.ConfirmNewEmailAddress)%> 
    <%= Html.ValidationMessageFor(m => m.ConfirmNewEmailAddress)%> 
    <br />

    <input type="submit" value="Submit" />

<% } %>

<script src="<%= ResolveUrl("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript" language="javascript"></script>
<script src="<%= ResolveUrl("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript" language="javascript"></script>
<script src="<%= ResolveUrl("~/Scripts/MicrosoftMvcValidation.js") %>" type="text/javascript" language="javascript"></script>