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.

CoolArchTek
  • 3,729
  • 12
  • 47
  • 76

1 Answers1

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>
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 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 Jul 17 '13 at 18:57
  • @CoolArchTek: in that case, have a look at this question: http://stackoverflow.com/questions/7340751/asp-net-mvc-validation-groups – Brad Christie Jul 17 '13 at 19:00