我有一些注册表单有2个otpions
- 个人
- 公司
醇>
他们的一些字段与密码,用户名,电子邮件和其他字段相同。
我的问题是我们应该在那种情况下实施哪种战略模式?
我们是否使用2个“TAB”和1个按钮“SUBMIT”但在这种情况下我们有重复的UI字段...我不明白在这种情况下应该如何使用MODEL类以及我们如何验证它...
或者...
我们需要使用2个按钮“SUBMIT”并以某种方式使用2个模型......
我知道我们可以使用if(代码跟随)但我们需要哪种MODEL?
<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>
我不知道我们可以使用哪种方法......
有任何线索吗?
谢谢!!!
答案 0 :(得分:1)
有几种方法,但存在的方法允许您不重复UI字段并具有单个提交按钮,您可以根据所选用户AccountType划分模型验证,自定义ActionMethodSelectorAttribute帮助您根据用户帐户划分方法类型。模型将在专有行动中自动验证。
以下是示例实现:
控制器:
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; }
}
自定义ActionMethodSelectorAttribute:
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 = "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"/>
}