Url.Action不会触发客户端验证

时间:2010-12-18 18:06:40

标签: jquery asp.net-mvc-2 validation client-side url.action

出于某种原因,我无法让Url.Action在MVC 2中触发客户端验证。我已将其设置为这样

<div class="createpage"><a href="<%= Url.Action("Create","Account") %>"></a></div>

其中'Create'是动作,'Home'是控制器(AccountController.cs)。如果我输入常规提交按钮,则验证工作非常完美。我有这样的验证设置

<% using (Html.BeginForm())
    {%>
    <%= Html.ValidationSummaryJQuery(
    "Please fix the following errors.",
    new Dictionary<string, object> { { "id", "valSumContainer" /* This is important. You can change the "id" attribute value, but don't remove it */ } }) %>
    <% ViewContext.FormContext.ValidationSummaryId = "valSumContainer"; { %>
//Form here
<%}%>

验证设置来自here

我收到验证消息的RegistrationModel.cs看起来像是

[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
public class RegistrationModel
{
    [Required(ErrorMessage = "Username is required")]
    [StringLength(25, ErrorMessage = "Username cannot be longer than 25 characters")]
    public string Username { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }
    public AccountType AccountType { get; set; }

    [Required(ErrorMessage = "Studio Name is required.")]
    public string StudioName { get; set; }

    [Required(ErrorMessage = "Phone number is required")]
    [PhoneNumber(ErrorMessage="Not a valid phone number")]
    public string PhoneNumber { get; set; }

    [Required(ErrorMessage = "Email address is required")]
    [Email(ErrorMessage = "Not a valid email address")]
    public string EmailAddress { get; set; }

    [Required(ErrorMessage = "City is required")]
    public string City { get; set; }

    [UIHint("StatesDropDown")]
    [Required(ErrorMessage="State is required")]
    public string State { get; set; }

    [Required(ErrorMessage = "Zip code is required")]
    [StringLength(10, ErrorMessage = "Zip code canot be longer than 10 digits")]
    [ZipCode(ErrorMessage="Invalid zip code")]
    public string ZipCode { get; set; }

    [Required(ErrorMessage = "About text is required")]
    public string AboutText { get; set; }

    [Required(ErrorMessage = "Starting price is required")]
    [Currency(ErrorMessage="Not a valid currency value")]
    public double PriceStartsAt { get; set; }

    [UIHint("SkillsListView")]
    [Required(ErrorMessage="At least 1 skill must be selected")]
    public IList<Skill> Skills { get; set; }

    [UIHint("EquipmentListView")]
    [Required(ErrorMessage = "At least 1 equipment must be selected")]
    public IList<Skill> Equipment { get; set; }

    [UIHint("OccassionsListView")]
    [Required(ErrorMessage = "At least 1 occassion must be selected")]
    public IList<Skill> Occassions { get; set; }
}

就像我说的,使用正常的提交按钮它可以正常工作(但客户端需要他们的UI,因此按钮必须使用CSS的某些图像精灵。

编辑:将 HomeController 代替 AccountController ,修复

2 个答案:

答案 0 :(得分:3)

@PsychoCoder

在网络上,只有在提交表单时才会触发验证。

如果您想使用锚标记进行验证,则需要设置一些javascript代码。这个javascript可能是jQuery验证库,或者您可以使用锚点上的onclick事件来调用提交。

<a href="javascript:void(0);" onclick="this.form[0].submit()">Create</a>

'this.form [0]'可以替换为创建表单的id。

答案 1 :(得分:0)

认为我越来越近了。将我的 BeginForm 更改为

Html.BeginForm("Create", "Account", FormMethod.Post, new { @id = "RegisterForm" })

制作了一个可点击的DIV标签,可以显示我需要的图像精灵

<div onclick="javascript:RegisterForm.submit(); return false" class="createpage">
&nbsp;
</div>

所以我已经提交了表单,但根本没有进行验证,即使空(必填)表单字段仍然提交。那么现在,这是令人困惑的哈哈。从未如此艰难地提交过&amp;验证表格(然后我再次成为MVC的新人)