ASP.NET - 在一个注册表中注册的两种类型的用户?

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

我正在构建一个网站应用程序,它将有两种不同类型的用户,我们称一种为 A,另一种为 B。它们有一些相似的数据,如:'name'、'password'等,其余的则不同。我已经分别为他们做了2个tables因为我需要这样,但是我有一个想法,我不确定我是否可以做到!

这个想法是,当用户进入注册页面时,他们会看到一个注册表单,其中包含 AB 之间相似的数据,然后我会让用户选中一个复选框,指示它是 A 用户还是 B 用户。根据他们的选择,表格的其余部分将出现在同一页面中,让他们继续注册。

我在 C# 中使用 ASP.NET,我想知道这个想法是否适用?我的问题是复选框 - 如何让注册表的其余部分根据他们选择的内容出现,然后将其添加到右侧 table?

正如@Fel 在评论中所说,

你最好使用单选按钮,

我们称它们为 rb1 和 rb2,通过为单选按钮指定相同的组名对它们进行分组。

并且还为两者提供 AutoPostBack="True",这样只有您可以在选中单选按钮时更改其余字段。

分别为两个用户创建其余表单,作为 A 的面板 p1 和 B 的 p2

在rb1_checkedchanged事件中,显示p1, 在 rb2_checkedchanged 事件中,显示 p2

点击提交按钮时

如果(rb1.checked=真)

显示表格A; 将其存储在 A 的 Table 中; 别的 B 的显示形式; 将其存储在 B 的 Table 中;

希望这对您有所帮助... 一切顺利...

MVC?

2 个选项:

要么在您的 html 中同时拥有两种形式,属性 ID = 'form a','form b'。确保将表单提交给不同的操作。 显示隐藏形式如下:

$('.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

此外,如果您想显示表格,请不要这样做 display:none 表单内有一个设置为不显示,直到用户做出选择。

--

使用 ajax,将您的表单作为局部视图,并在单击收音机后将它们上传到目标 div。 (如果需要,请告诉我们)

我认为第一个显示/隐藏就足够了。没有理由上传,因为表单只是一组空输入。

编辑

接下来,我们在您的控制器中捕获这些提交的表单。每个表单将提交相同的操作,或者您希望执行不同的操作,这没有区别 - 您的偏好。

选项 1。 页面上的表格:

 <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>

控制器

    [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);
    }

选项 2。 使用模型。 型号

        public class YourRegisterAction
    {
        public string  Name { get; set; }
        public string Password { get; set; }
        public char FormType { get; set; }
    }

景色

@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>

控制器

        [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);
    }

在控制器中提交表单之后。像往常一样坚持在数据库中。

另外请使用@using (Html.BeginForm) 而不是表单标签。您可以在此处找到大量相关信息。