首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewModel在asp.net mvc-5中可以用于登录吗?

ViewModel在asp.net mvc-5中可以用于登录吗?
EN

Stack Overflow用户
提问于 2020-11-22 18:53:13
回答 1查看 123关注 0票数 0

在我的asp.net项目中,我使用一个名为DoctorLoginViewModel.cs的ViewModel,它用于呈现Login视图。我想知道这个ViewModel是否可以用于登录ActionMethod。

凭据在doc_cred表中

DoctorLoginViewModel:

代码语言:javascript
复制
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下拥有的内容:

代码语言:javascript
复制
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中拥有的内容:

代码语言:javascript
复制
[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:

代码语言:javascript
复制
    @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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-22 18:57:51

代码语言:javascript
复制
[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();
}

请尝尝这个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64958174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档