MVC?
2 tùy chọn:
Có cả hai biểu mẫu trong html của bạn, với ID thuộc tính = 'form a', 'form b'. Đảm bảo gửi biểu mẫu cho các hành động khác nhau. Hiện ẩn hoặc hình thức như thế này:
$('.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
Ngoài ra, nếu bạn muốn hiển thị các hình thức chỉ không làm display: none bên trong biểu mẫu có một mà được thiết lập để không hiển thị cho đến khi người dùng lựa chọn.
-
HOẶC
Sử dụng ajax, có các hình thức của bạn như là quang cảnh một phần và tải chúng vào một div mục tiêu một lần radio được nhấp. (hãy cho chúng tôi biết nếu điều này là cần thiết)
Tôi nghĩ chương trình/ẩn đầu tiên là đủ cho trường hợp của bạn. Không có lý do gì để tải lên vì biểu mẫu chỉ là một bộ đầu vào trống.
EDIT
Tiếp theo, chúng ta nắm bắt những hình thức nộp trong điều khiển của bạn. Mỗi hình thức sẽ gửi cùng một hành động, hoặc bạn muốn các hành động khác nhau, điều này không tạo sự khác biệt - tùy chọn của bạn.
tùy chọn 1. Hình thức trên trang:
<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>
Bộ điều khiển
[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);
}
tùy chọn 2. Sử dụng các mô hình. Mô hình
public class YourRegisterAction
{
public string Name { get; set; }
public string Password { get; set; }
public char FormType { get; set; }
}
Quan điểm
@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>
Bộ điều khiển
[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);
}
Sau khi bạn đã hình thức nộp trong điều khiển của bạn. Chỉ cần duy trì trong DB như bình thường.
Cũng vui lòng sử dụng @using (Html.BeginForm) thay vì thẻ biểu mẫu. Bạn có thể tìm thấy nhiều thông tin về điều này ở đây.
Nút radio sử dụng tốt hơn để người dùng chỉ có thể chọn một nút. Nếu (radiobutton1.checked = true) // hiển thị người dùng một biểu mẫu else // hiển thị người dùng b biểu mẫu – missellorin
ý tưởng của bạn thực sự có thể áp dụng. – dada