asp.netmvc-5中的ViewModel可以用于登录吗?
Can ViewModel in asp.net mvc-5 be used for login?
在我的 asp.net 项目中,我使用了一个名为 DoctorLoginViewModel.cs 的 ViewModel,它用于呈现登录视图。
我想知道这个 ViewModel 是否可以用于登录 ActionMethod
凭据在 doc_cred table
DoctorLoginViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace das.Models.ViewModel.Doctor
{
[Table("doc_cred")]
public class DoctorLoginViewModel
{
[Required]
[DisplayName("Email")]
public string doc_email { get; set; }
[Required]
[DisplayName("Password")]
public string doc_pass { get; set; }
[Required]
[DisplayName("Confirm Password")]
[Compare("Password")]
public string confirm_doc_pass { get; set; }
}
}
这是我在doc_cred.cs下的内容:
namespace das.Models.DataModel
{
using System;
using System.Collections.Generic;
public partial class doc_cred
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public doc_cred()
{
this.doc_personal = new HashSet<doc_personal>();
}
public string doc_email { get; set; }
public string doc_pass { get; set; }
public System.DateTime doc_acc_creation { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<doc_personal> doc_personal { get; set; }
}
}
这是我目前在 DoctorController.cs:
[HttpPost]
public ActionResult Login(doc_cred dl)
{
var login = db.doc_cred.Where(a => a.doc_email.Equals(dl.doc_email) &&
a.doc_pass.Equals(dl.doc_pass)).FirstOrDefault();
if(login != null)
{
return RedirectToAction("Dashboard","Doctor");
}
return View();
}
现在我的问题是我可以使用我的 ViewModel 进行登录吗?
如果有怎么办?
编辑:这是我的 Login.cshtml:
@model das.Models.ViewModel.Doctor.DoctorLoginViewModel
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DoctorLoginViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.doc_email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.doc_email, new { htmlAttributes
= new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.doc_email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.doc_pass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.doc_pass, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.doc_pass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.confirm_doc_pass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.confirm_doc_pass, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.confirm_doc_pass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
[HttpPost]
public ActionResult Login(DoctorLoginViewModel dl)
{
var login = db.doc_cred.Where(a => a.doc_email.Equals(dl.doc_email) &&
a.doc_pass.Equals(dl.doc_pass)).FirstOrDefault();
if(login != null)
{
return RedirectToAction("Dashboard","Doctor");
}
return View();
}
请试试这个。
在我的 asp.net 项目中,我使用了一个名为 DoctorLoginViewModel.cs 的 ViewModel,它用于呈现登录视图。 我想知道这个 ViewModel 是否可以用于登录 ActionMethod
凭据在 doc_cred table
DoctorLoginViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace das.Models.ViewModel.Doctor
{
[Table("doc_cred")]
public class DoctorLoginViewModel
{
[Required]
[DisplayName("Email")]
public string doc_email { get; set; }
[Required]
[DisplayName("Password")]
public string doc_pass { get; set; }
[Required]
[DisplayName("Confirm Password")]
[Compare("Password")]
public string confirm_doc_pass { get; set; }
}
}
这是我在doc_cred.cs下的内容:
namespace das.Models.DataModel
{
using System;
using System.Collections.Generic;
public partial class doc_cred
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public doc_cred()
{
this.doc_personal = new HashSet<doc_personal>();
}
public string doc_email { get; set; }
public string doc_pass { get; set; }
public System.DateTime doc_acc_creation { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<doc_personal> doc_personal { get; set; }
}
}
这是我目前在 DoctorController.cs:
[HttpPost]
public ActionResult Login(doc_cred dl)
{
var login = db.doc_cred.Where(a => a.doc_email.Equals(dl.doc_email) &&
a.doc_pass.Equals(dl.doc_pass)).FirstOrDefault();
if(login != null)
{
return RedirectToAction("Dashboard","Doctor");
}
return View();
}
现在我的问题是我可以使用我的 ViewModel 进行登录吗? 如果有怎么办?
编辑:这是我的 Login.cshtml:
@model das.Models.ViewModel.Doctor.DoctorLoginViewModel
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DoctorLoginViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.doc_email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.doc_email, new { htmlAttributes
= new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.doc_email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.doc_pass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.doc_pass, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.doc_pass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.confirm_doc_pass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.confirm_doc_pass, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.confirm_doc_pass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
[HttpPost]
public ActionResult Login(DoctorLoginViewModel dl)
{
var login = db.doc_cred.Where(a => a.doc_email.Equals(dl.doc_email) &&
a.doc_pass.Equals(dl.doc_pass)).FirstOrDefault();
if(login != null)
{
return RedirectToAction("Dashboard","Doctor");
}
return View();
}
请试试这个。