0

I have a form with many fields which I post back using the standard form tag:

@using (Html.BeginForm("SubmitForm", "ControllerName", FormMethod.Post))
{
    //fields and submit button
}

Also on my form I have a grid which lists contractors, and I have a a button which adds contractors to the list. The way I do it is to create the button as a submit and use the form submit action to do the work. I'm pretty sure this isn't the best way to go about it. Here's is the action on the controller:

[HttpPost]
public ActionResult SubmitForm(string btnSubmit, SupplySelectionViewModel model)
{
    switch (btnSubmit)
    {
        case "addContractor":

            if (model.ContractorId > 0)
            {
                //add contractor to parent record and reload whole page

                return RedirectToAction("Edit", new { id = model.ProposalId });
            }
            else
            {
                ModalState.AddModelError("Select a contractor");

                //Return view with error
                return View("Edit", model);
            }

        case "save":

            if (ModelState.IsValid)
            {
                //Save whole model and redirect back to home page
            }

            //Return to view with validation erros
            return View("Edit", model);
    }
}

As you can see I use the name of the button to decide what to do (add contractor or submit the form). What I would like is to have the add contractor as an actionlink and post to a separate action. How would I do this without passing the whole model back as I add a model error if no contractor has been selected? I know I could do the validation using jquery but let's just say I want to keep all validation server side for now.

Imad Alazani
  • 6,688
  • 7
  • 36
  • 58
Wilky
  • 199
  • 1
  • 3
  • 18
  • http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework check this – Nitin Varpe Aug 13 '13 at 09:37
  • Thanks for the input Nitin but query isn't how to use multiple submit buttons but if the 'add contractor' button should even be a submit button. It feels like a hack. I was asking if there was a way to just post to an action but still be able to add model errors and pass the model back to the view etc. – Wilky Aug 13 '13 at 09:57
  • Check this http://stackoverflow.com/questions/4642845/asp-net-mvc-how-to-preserve-modelstate-errors-across-redirecttoaction – Nitin Varpe Aug 13 '13 at 13:00

0 Answers0