c# – ASP.NET – 在一种注册表单中注册的两种类型的用户?
我正在构建一个具有两种不同类型用户的网站应用程序,让我们调用一个A,另一个是B.它们有一些类似的数据,例如:’name’,’password’等,其余的都是不同.我已经分别为他们做了两张桌子,因为我需要它,但我有一个想法,我不确定我是否可以做到!
这个想法是当用户进入注册页面时,他们将显示一个注册表单,其中包含A和B之间类似的数据,然后我会让用户选中一个复选框,指示它是A用户还是一个B用户.根据他们选择的内容,表单的其余部分将显示在同一页面中,以便他们继续注册. 我在C#中使用ASP.NET,我想知道这个想法是否适用?我的问题在于复选框 – 如何根据他们选择的内容显示其余的注册表单,然后将其添加到右表中? 解决方法
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); } 在控制器中提交表单后.像往常一样坚持DB. 另外请使用@using(Html.BeginForm)而不是表单标签.你可以在这里找到很多信息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |