ASP.NET - two types of users who register in one registration form?

I am creating a website application that will have two different types of users, let one A and the other B They have some similar data, such as: "name", "password", etc., and the rest are different. I made 2 tables for them separately, because I need it, but I have an idea, and I'm not sure if I can do it!

The idea is that when the user goes to the registration page, they will be shown a registration form containing data close between A and B , and then I will allow the user to check the box indicating whether he is user A or user B Depending on what they have chosen, the rest of the form will appear on the same page to continue registration.

I work with ASP.NET in C # and I wonder if this idea is applicable? My problem is with the checkbox - how can I leave the rest of the registration form depending on what they have chosen, and then add it to the desired table?

+7
c # database registration
source share
2 answers

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(); //alert(formType); if (formType == "A") { $('#FormA').show(); $('#FormB').hide(); } else { $('#FormB').show(); $('#FormA').hide(); } }); 
 <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"/> <!--place B for B form--> <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.

+3
source share

As @Fel said in a comment,

It’s better to use radio buttons,

Let them be called rb1 and rb2, grouping the switches, giving them the same group name.

And also give AutoPostBack = "True" for both so that only you can change the rest of the fields while the radio camera is checked.

Create the remaining forms for both users separately as Panels p1 for A and p2 for B

Show p1 in the rb1_checkedchanged event, show p2 in the rb2_checkedchanged event

When you click Submit

if (rb1.checked = true)

display form for A; keep it in the table for A; another display form for B; keep it in the table for B;

Hope this helps ... All the best ...

+1
source share

All Articles