Reputation: 37633
I have got some sign up form that has 2 otpions
- Personal
- Company
Some of the fields for them are the same like Password, Username, Email and other are different.
My question is which strategy of the MODEL we should implement in that case?
Have we i.e. use 2 "TABs" and 1 button "SUBMIT" but in that case we have duplicated UI fields... And I don't understand how the class MODEL should be in that case and how we have to validate it...
Or...
We need to use 2 buttons "SUBMIT" and use somehow 2 MODELS ....
I know we can use if (code is following) but which MODEL we have need for it?
<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>
Well I don't know which approach we can use...
Any clue?
Thank you!!!
Upvotes: 0
Views: 1283
Reputation: 7385
There is several approaches, but exists approach which which allow you don't duplicate UI fields and have single submit button, you can divide your model validation depending on selected user AccountType, custom ActionMethodSelectorAttribute help you to divide methods depending on user account type. Model will be automatically validated in appropriative action.
Here is sample implementation:
controller:
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
});
}
}
model:
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; }
}
custom ActionMethodSelectorAttribute:
public class SignUpSelector : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return (controllerContext.HttpContext.Request.Form["AccountType"] + "Registration" == methodInfo.Name);
}
}
view:
@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"/>
}
Upvotes: 1