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.