2

My view has a textbox(Email) and a submit button.

@using (Html.BeginForm("FindMyDetail", "ResetPassword", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
   {
      @Html.AntiForgeryToken()
      <h4>Enter your email.</h4>

     @Html.ValidationSummary(false, "", new { @class = "alert alert-danger" })

 @Html.ValidationMessageFor(m=>m.Email)
<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @*@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @value = @Html.DisplayFor(m => m.Email) })*@
        @Html.TextBoxFor(m => m.Email, true, htmlAttributes: new { @class = "form-control", placeholder = "Enter Email Id you used to register" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">

        <input type="submit" class="btn btn-default" value="Email Link" />

    </div>
</div>

}

I have to show multiple validation messages in a view of MVC web app.

Case1 : Show validation message to enter email id when textbox left empty.

Case2 : Submit button triggers a mail if email exists .If mail fails(false) a validation message that email does not exist should come. On click of submit button takes to a controller which triggers a mail to given email. If succesfull i will return a different view with success message else i will return the same view (EmailLink view)

In asp.net webforms implementing this seems easy but looks very different and am puzzled how to implement this in MVC since am new to it.

EDIT: I need it to be implemented using Custom Validation.Not using data anotations.

kaarthick raman
  • 793
  • 2
  • 13
  • 41

1 Answers1

0

There is two easy ways for doing this, you can validate in client side or server side.

client side:

Import this jquery plugin in your page: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js

And on your javascript file, you can create the validations you desire, for example:

$(document).ready(function() {
  $("#FindMyDetail").validate({
    rules: {
      Email: {
        required: true,
        maxlength: 40,
        email: true
      }
    },
    messages: {
      Email: {
        required: "We need your email address to contact you",
        email: "Your email address must be in the format of name@domain.com"
      }
    }
  });
});

Server side

You can use the asp.net framework and let it validate your model. You have to decorate your model with some attributes, like:

public class Person
{
    public int ID { get; set; }

    [Required]
    [StringLength(40)]
    public string Email { get; set; }
}

And in your controller's action:

[HttpPost]
public ActionResult Create(Person person)
{
    if (ModelState.IsValid)
    {
        //foo
        return RedirectToAction("Index");
    }

    return View(person);
}
gnllucena
  • 804
  • 7
  • 6