3

I have 4 button on the form, 'Submit 1' calls controller A, 'Submit 2' calls controller B.

I also have 'Button 1' and 'Button 2' on the same form and I don't want validation to happen/fire for these.

How do I make 'Submit 1' and 'Submit 2' to validate the form and not the 'Button 1' and 'Button 2' ?

At the same time 'Button 1' and 'Button 2' should submit to a different controllers. How do I achieve this?

public class SampleForm
{
    [Display(Name = "FName: ")]
    [Required(ErrorMessage = "Enter FName")]
    public string FirstName { get; set; }

    [Display(Name = "LName: ")]
    [Required(ErrorMessage = "Enter LName")]
    public string LastName { get; set; }

    [Display(Name = "Bypassid: ")]
    [Required(ErrorMessage = "Enter Bypassid")]
    public string Bypassid { get; set; }    
}

@model SampleForm
@using (Html.BeginForm("SampleForm", "SampleController", FormMethod.Post, new { name = "frmSample", id = "frmSample" }))
{
    @Html.LabelFor(model => model.FirstName)
    @Html.TextBoxFor(model => model.FirstName)
    @Html.ValidationMessageFor(model => model.FirstName)

    @Html.LabelFor(model => model.LastName)
    @Html.TextBoxFor(model => model.LastName)
    @Html.ValidationMessageFor(model => model.LastName)

    @Html.LabelFor(model => model.Bypassid)
    @Html.TextBoxFor(model => model.Bypassid)
    @Html.ValidationMessageFor(model => model.Bypassid)

    <input type="submit" value="Submit 1" name="Command" />

    <input type="submit" value="Submit 2" name="Command" />

    <button type="button" name="Button 1" value="Button 1">Button 1</button>

    <button type="button" name="Button 2" value="Button 2">Button 2</button>
}

I want 'Submit 1' and 'Submit 2' to validate the form using DataAnnotations, but when I click 'Button 1' I want to make sure there is value in the field 'Bypassid' and redirect to another controller (Say . and when I click 'Button 2' I don't to validate anything and just redirect to another controller.

4
  • Are these butttons on same form or different forms ? and you have used for validation? jQuery validation or DataAnnotations? Commented Jun 7, 2013 at 3:17
  • I have all the buttons on the same form. I am using DataAnnotations for validation. Commented Jun 7, 2013 at 13:41
  • Could you please provide the exact Html that you are using? thanks! Commented Jun 7, 2013 at 17:34
  • I updated the main post/initial post with the code and details. Commented Jul 17, 2013 at 18:19

1 Answer 1

2

Regarding submitting to two actions, you have the right idea with two submit buttons and independent values. You can use the values within the action to decipher how you want to handle the request. Though you will only be submitting to one action at the end of the day, you can redirect it within the action to handle the variance of processes. e.g.

@using (Html.BeginForm("BasicAction"))
{
    @* controls *@
    <input type="submit" value="action1" name="command" />
    <input type="submit" value="action2" name="command" />
}

Then, your controller:

[HttpPost]
public ActionResult BasicAction(MyModel model, String command)
{
    if (ModelState.IsValid)
    {
        switch (command)
        {
            case "action1":
                return RedirectToAction("Action1", new { model = model });
            case "action2":
                return RedirectToAction("Action2", new { model = model });
        }
    }
    return View(model);
}

// handle action1
[NonAction]
public ActionResult Action1(MyModel model)
{
    // do whatever
}

[NonAction]
// handle action2
public ActionResult Action2(MyModel model)
{
    // do whatever
}

As far as disabling validation, you can set disableValidation=true on button 1 & 2 and the client-side validation will be ignored (I believe this was as of MVC2). To do this, add the following javascript after those buttons are defined:

<script>
  // IDs of the buttons to be disabled
  var disabled = ['button1','button2'];
  for (var i = 0; i < disabled.length; i++){
      document.getElementById(disabled[i]).disableValidation = true;
  }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Brad. In my case I don't want to disable validation for let's say 'Button 1' and I want 'Button 1' to validate only one field on the for instead of three. I try to find my side as well. Thanks Again!
@CoolArchTek: in that case, have a look at this question: stackoverflow.com/questions/7340751/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.