MVC?
2 options:
Both have both forms in your html, with attribute ID = 'form a', 'form b'. Be sure to submit forms for different activities. Show hidden forms:
$('.radioBtn').click(function() { var formType = $(this).val();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <form style="display: none" id="FormA" action="/actionA"> .... your html FORM A </form> <form style="display: none" id="FormB" action="/actionB"> .... your html FORM B </form> <input type="radio" name="typeOfForm" class="radioBtn" value="A">Form A <input type="radio" name="typeOfForm" class="radioBtn" value="B">Form B
In addition, if you want to show forms, just do not show: none inside the form there is a parameter that is not displayed until the user makes a choice.
-
OR
Use ajax, create your forms as partial views and load them into the target div after clicking on the radio. (let us know if necessary)
I think the first show / hide is enough for your business. There is no reason to download, since the form is just an empty set of inputs.
EDIT
Then we will catch these submitted forms in your controller. Each form will be subject to the same action or you need different actions, it does not matter - your preferences.
option 1. The form on the page:
<form action='@Url.Action("YourRegisterAction", "controller")' > <input type="hidden" name="FormType" value="A"/> <input type="text" name="Name" placeholder ="enter your name"/> <input type="text" name="Password" placeholder ="enter your name"/> <input type="submit" value="Register Me!"/> </form>
Controller
[HttpPost] public ActionResult YourRegisterAction(char formType, string name, string password) { if (formType == 'A') bool success = BL.Server.Instance.SaveMyNewUserToDBTypeA(name, password); else if (formType == 'B') bool success = BL.Server.Instance.SaveMyNewUserToDBTypeB(name, password); return View("ThankYou", success); }
option 2. Use the model. Model
public class YourRegisterAction { public string Name { get; set; } public string Password { get; set; } public char FormType { get; set; } }
View
@model Domain.UI.Models <form action='@Url.Action("YourRegisterAction", "controller")' > @Html.HiddenFor(m=>m.FormType) @Html.TextBoxFor(m=>m.Name) @Html.TextBoxFor(m=>m.Password) <input type="submit" value="Register Me!"/> </form>
Controller
[HttpPost] public ActionResult YourRegisterAction(RegisterViewModel m) { if (m.FormType == 'A') bool success = BL.Server.Instance.SaveMyNewUserToDBTypeA(m.Name, m.Password); else if (m.FormType == 'B') bool success = BL.Server.Instance.SaveMyNewUserToDBTypeB(m.Name, m.Password); return View("ThankYou", success); }
After you submitted the form in your controller. Just continue to the database as usual.
Also use @using (Html.BeginForm) instead of form tags. You can find a lot of information here.