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

use*_*119 7 c# database asp.net registration

我正在构建一个具有两种不同类型用户的网站应用程序,让我们调用一个A,另一个是B.它们有一些类似的数据,例如:'name','password'等,其余的则不同.我已经分别为他们做了两张桌子,因为我需要它,但我有一个想法,我不确定我是否可以做到!

我们的想法是,当用户进入注册页面,他们将显示包含之间类似的数据登记表AB,然后我会让用户检查指示是否它是一个复选框A用户或B用户.根据他们选择的内容,表单的其余部分将显示在同一页面中,以便他们继续注册.

我在C#中使用ASP.NET,我想知道这个想法是否适用?我的问题是复选框 - 如何根据他们选择的内容显示其余的注册表单,然后将其添加到正确的表格中?

Den*_*nys 4

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();
  }
});
Run Code Online (Sandbox Code Playgroud)
<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
Run Code Online (Sandbox Code Playgroud)

另外,如果您想显示表单,请不要执行 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>
Run Code Online (Sandbox Code Playgroud)

控制器

    [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);
    }
Run Code Online (Sandbox Code Playgroud)

选项 2. 使用模型。该模型

        public class YourRegisterAction
    {
        public string  Name { get; set; }
        public string Password { get; set; }
        public char FormType { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

风景

@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>
Run Code Online (Sandbox Code Playgroud)

控制器

        [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);
    }
Run Code Online (Sandbox Code Playgroud)

在控制器中有提交的表单后。只需像平常一样保留在数据库中即可。

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