In my asp.net project I'm using a ViewModel called DoctorLoginViewModel.cs which is used for rendering the Login view. I was wondering if this ViewModel can be used in login ActionMethod
Credentials are in 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; }
}
}
This is what I have under 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; }
}
}
This is what I currently have in my 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();
}
Now my question is can I use my ViewModel for login? If so how?
EDIT: Here's my 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();
}
Please try this.