我要做的是创建一个登录页面,其中包含两个不同类型用户的登录屏幕。
对于One Login Screen,我这样做了 -
我创建了一个像 -
这样的模型public class User1{
[Required(ErrorMessage = "MSG1", AllowEmptyStrings = false)]
public string Id {get; set;}
[Required(ErrorMessage = "MSG2", AllowEmptyStrings = false)]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
public string Password {get; set;}
}
表格 -
@model Test.Models.User1
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
if (@ViewBag.Message != null)
{
<div style="border: 1px solid red">
@ViewBag.Message
</div>
}
<table>
<tr>
<td>@Html.LabelFor(a => a.Id)</td>
<td>@Html.TextBoxFor(a => a.Id)</td>
<td>@Html.ValidationMessageFor(a => a.Id)</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Password)
</td>
<td>
@Html.PasswordFor(a => a.Password)
</td>
<td>
@Html.ValidationMessageFor(a => a.Password)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Login" />
</td>
<td></td>
</tr>
</table>
}
控制器是 -
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User1 u)
{
if (ModelState.IsValid)
{
//My Code
}
return View(u);
}
我想在此处为User2 Type添加另一个登录屏幕。这可以在同一个视图上实现吗?
答案 0 :(得分:0)
如果每个用户类型用于登录的字段之间没有差异(即,如果它们都使用Id和密码),那么我建议使用“用户”和“用户”。要绑定到控制器上的模型,然后在控制器中处理如何确定它是什么类型的用户并返回适当的视图。
或者,如果您不能这样做,您只需添加第二个登录表单,该表单将发布到控制器上的其他操作(即LoginUserType2)。例如:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LoginUserType1(User1 u)
{
if (ModelState.IsValid)
{
//My Code
}
return View(u);
}
..
..
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LoginUserType2(User1 u)
{
if (ModelState.IsValid)
{
//My Code
}
return View(u);
}
编辑:
我这样做的方法是创建一个表示页面上所有字段的UserViewModel类。例如:
public class UserViewModel
{
public string Username { get; set; }
public int UserId { get; set; }
public string PasswordType1 { get; set; }
public string PasswordType2 { get; set; }
}
然后可以将其用作页面的模型,您可以将其发布到以下两个操作中:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LoginUserType1(UserViewModel u)
{
if (ModelState.IsValid)
{
// Login with UserId and PasswordType1
}
return View(u);
}
..
..
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LoginUserType2(UserViewModel u)
{
if (ModelState.IsValid)
{
// Login with Username and PasswordType2
}
return View(u);
}
每个表单都会发布到两个操作之一,您可以根据需要使用ViewModel中的任何字段。它并不理想,但我认为它可以满足您的需求。
答案 1 :(得分:0)
我在ViewBag的帮助下这样做了。首先在您的控制器中 使用 ViewBag 创建动态属性,就像您的用户是type1然后
一样public ActionResult Index()
{
//check your user type and set it to true/false
ViewBag.UserType1 = true;
}
if(ViewBag.UserType1 != null)
{
@if (Convert.ToBoolean(ViewBag.UserType1))
{
//Return view for user of type1
}
else
{
//return view for user of type2
}
}
答案 2 :(得分:0)
您应该使用编辑器模板,它们允许分层显示数据。例如,您可以这样做:
public class User1 {}
public class User2 {}
public class Login
{
public object User { get; set; }
}
Login.cshtml:
@model Login
<!-- container html for login screen -->
<div>
@Html.EditorFor(m => m.User);
</div>
/EditorTemplates/User1.cshtml:
@model User1
@using (Html.BeginForm("LoginUser1", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
if (@ViewBag.Message != null)
{
<div style="border: 1px solid red">
@ViewBag.Message
</div>
}
<table>
<tr>
<td>@Html.LabelFor(a => a.Id)</td>
<td>@Html.TextBoxFor(a => a.Id)</td>
<td>@Html.ValidationMessageFor(a => a.Id)</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Password)
</td>
<td>
@Html.PasswordFor(a => a.Password)
</td>
<td>
@Html.ValidationMessageFor(a => a.Password)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Login" />
</td>
<td></td>
</tr>
</table>
}