C# 基于RadioButton(MVC)验证字段
我有一些有两个OTPION的报名表C# 基于RadioButton(MVC)验证字段,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我有一些有两个OTPION的报名表 私人的 公司 它们的一些字段是相同的,如密码、用户名、电子邮件和其他不同的字段 我的问题是,在这种情况下,我们应该实施哪种模型策略 我们是否使用了2个“选项卡”和1个按钮“提交”,但在这种情况下,我们有重复的UI字段。。。我不明白在这种情况下,类模型应该是怎样的,我们必须如何验证它 或者 我们需要使用2个按钮“提交”,并以某种方式使用2个模型 我知道我们可以使用if(代码如下),但我们需要哪个模型 <html> ... <body>
<html>
...
<body>
<div>
<form action="/SignUp/Personal" method="post">
<input type="text" name="username" value="" />
<input type="text" name="passowrd" value="" />
<input type="submit" name="signup" value="SUBMIT" />
</form>
<form action="/SignUp/Company" method="post">
<input type="text" name="username" value="" />
<input type="text" name="passowrd" value="" />
<input type="submit" name="signup" value="SUBMIT" />
</form>
</div>
</body>
</html>
...
我不知道我们可以用哪种方法
有线索吗
谢谢你 有几种方法,但现有的方法允许您不复制UI字段并使用单个提交按钮,您可以根据所选用户帐户类型划分模型验证,自定义操作方法Selector属性帮助您根据用户帐户类型划分方法。模型将在专用操作中自动验证 以下是示例实现: 控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new SignUpModel
{
Common = new Common(),
Personal = new Personal(),
Company = new Company()
});
}
[HttpPost]
[SignUpSelector]
[ActionName("Index")]
public ActionResult PersonalRegistration(Personal personal, Common common)
{
if (ModelState.IsValid)
{
//your code
}
return View("Index", new SignUpModel()
{
Common = common,
Personal = personal,
Company = new Company()
});
}
[HttpPost]
[SignUpSelector]
[ActionName("Index")]
public ActionResult CompanyRegistration(Company company, Common common)
{
if(ModelState.IsValid)
{
//your code
}
return View("Index", new SignUpModel()
{
Common = common,
Personal = new Personal(),
Company = company
});
}
}
型号:
public class SignUpModel
{
public string AccountType { get; set; }
public Common Common { get; set; }
public Company Company { get; set; }
public Personal Personal { get; set; }
}
public class Company
{
[Required]
public string CompanyName { get; set; }
public string Address { get; set; }
}
public class Personal
{
[Required]
public string FirstName { get; set; }
public int Age { get; set; }
}
public class Common
{
[Required]
public string UserName { get; set; }
[Required]
public string Passwrod { get; set; }
}
自定义操作方法SelectorAttribute:
public class SignUpSelector : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return (controllerContext.HttpContext.Request.Form["AccountType"] + "Registration" == methodInfo.Name);
}
}
视图:
@model MvcModelValidationTest.Controllers.SignUpModel
@{
ViewBag.Title=“主页”;
}
@使用(Html.BeginForm())
{
@Html.Display(“个人”)
@Html.radioButton(x=>x.AccountType,“个人”,新的{@checked=“checked”})
@Html.Display(“公司”)
@Html.radioButton(x=>x.AccountType,“公司”)
@Html.TextBoxFor(x=>x.Common.UserName)
@Html.PasswordFor(x=>x.Common.Passwrod)
@Html.TextBoxFor(x=>x.Company.CompanyName)
@Html.TextBoxFor(x=>x.Company.Address)
@Html.TextBoxFor(x=>x.Personal.FirstName)
@Html.TextBoxFor(x=>x.Personal.Age)
}
U给出了一个非常棒的答案,兄弟!我将向上++++你的答案。有一个有趣的男人!!!
@model MvcModelValidationTest.Controllers.SignUpModel
@{
ViewBag.Title = "Home Page";
}
@using(Html.BeginForm())
{
@Html.Display("Personal")
@Html.RadioButtonFor(x=>x.AccountType, "Personal",new { @checked = "checked" })<br/>
@Html.Display("Company")
@Html.RadioButtonFor(x=>x.AccountType, "Company")<br/>
@Html.TextBoxFor(x=>x.Common.UserName)<br/>
@Html.PasswordFor(x=>x.Common.Passwrod)<br/>
@Html.TextBoxFor(x=>x.Company.CompanyName)<br/>
@Html.TextBoxFor(x=>x.Company.Address)<br/>
@Html.TextBoxFor(x=>x.Personal.FirstName)<br/>
@Html.TextBoxFor(x=>x.Personal.Age)<br/>
<input type="submit"/>
}