开发者

Url.Action not triggering client-side validation

开发者 https://www.devze.com 2023-01-31 18:57 出处:网络
For some reason I cannot get Url.Action to trigger client-side validation in MVC 2. I have it set up like this

For some reason I cannot get Url.Action to trigger client-side validation in MVC 2. I have it set up like this

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

Where 'Create' is the action and 'Home' is the controller (AccountController.cs). If I put in a regular submit button the validation works just perfectly. I have the validation set up like so

<% 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
<%}%>

Validation set up is from here

The RegistrationModel.cs that I'm getting the validation messages from looks like so

[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")]
    p开发者_JAVA技巧ublic 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; }
}

Like I said, with a normal submit button it works just fine (but the client wants their UI so the buttons have to use certain image sprites with CSS.

EDIT: Put HomeController instead of AccountController, fixed


@PsychoCoder

On the Web, Validation is only fired when a form is submitted.

If you would like to validate using an anchor tag you will need to set up some javascript code. This javascript could be the jQuery Validation Library, alternatively you could call the submit using the onclick event on the anchor.

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

'this.form[0]' could be replaced with the id of the creation form.


I think I'm getting closer. Changed my BeginForm to look like

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

Made a click-able DIV tag that would display the image sprite I needed

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

So I've got it to submit the form, but no validation is performed at all, even with empty (required) form fields is submits anyways. So what now, this is getting confusing lol. Never had such a hard time submitting & validating a form (then again I'm new at MVC)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号