asp.net mvc中是否存在登录和注册的问题?

问题描述 投票:0回答:1

我是一个学习MVC的学生。

我只有一个表用于登录和注册。

问题是登录不起作用以及注册不起作用

“在此处输入图像描述”

stanstuds.cs

    public class stanstuds
    {
        [Key]
        public int id { get; set; }

        public string fname { get; set; }

        public string mname { get; set; }

        public string lname { get; set; }

        public string country { get; set; }

        public string usertype { get; set; }

        public string username { get; set; }

        public string password { get; set; }
    }

上下文类

     public class stanstudcontext:DbContext
    {
        public stanstudcontext():base("cnnos")
        {

        }

        public DbSet<stanstuds> studs { get; set; }
    }

控制器

public class LogRegController : Controller
    {
        stanstudcontext context = new stanstudcontext();

        //admin view just I print the sample message this is an admin view
        public ActionResult Index()
        {
            return View();
        }

        //client view just I print the sample message this is a client view
        public ActionResult Clientview()
        {
            return View();
        }
        //Login
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(stanstuds studs)
        {
            var userdata=context.studs.Where(x => x.username == studs.username && x.password == studs.password).FirstOrDefault();
            if (userdata != null)  //when i m debugging this condition is not check
            {
                  // i have a 2 records of admin in database but when i run time enter that record login is not working 
                if (userdata.usertype == "admin")   
                {
                    return RedirectToAction("Index", "LogReg");
                }
                else
                {
                    return RedirectToAction("Clientview", "LogReg");
                }

            }
            else
            {

                return View();
            }
        }

        //Registration 
        public ActionResult Regis()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Regis(stanstuds studs)
        {
            context.studs.Add(studs);
            context.SaveChanges();
            return View();
        }
    }

Login.cshtml

@model LoginRegOneTable.Models.stanstuds

@{
    ViewBag.Title = "Login";
}

<h2>Login</h2>

<html>
<head>
    <title>Demo Website</title>
</head>
<body>

    @using (Html.BeginForm())
    {
        <div>
            <label>UserName:</label>
            @Html.EditorFor(model => model.username)
        </div>
        <div>
            <label>Password:</label>
            @Html.EditorFor(model => model.password)
        </div>
        <div>
            <input type="submit" value="Login" />
        </div>
    }

</body>
</html>

Regis.cshtml

@{
    ViewBag.Title = "Regis";
}

<h2>Regis</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>stanstuds</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.fname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.fname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.fname, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.mname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.mname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.mname, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.lname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.lname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.lname, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.country, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.country, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.country, "", new { @class = "text-danger" })
            </div>
        </div>

         **I think the problem is here I want to insert by default entry as a user but how???** 



        <div class="form-group">
            @Html.LabelFor(model => model.usertype, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.usertype, new { htmlAttributes = new { @class = "form-control" ,@value = "User"} }) 
                @Html.ValidationMessageFor(model => model.usertype, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.username, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.username, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

[当用户注册然后默认情况下以用户身份插入时,当我按下注册按钮时,我的注册不起作用,然后未插入记录;当我按下登录名时,登录不起作用?我的脚本出了点问题??请帮助吗?

c# asp.net view model controller
1个回答
0
投票
        var userdata=context.studs.Where(x => x.username.Equals(studs.username)).FirstOrDefault();
            if (userdata != null)
            {
                    if (userdata.Password == userdata.Password) //success
                    {
                        HttpContext.Session.SetInt32("id", userdata.id);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ViewBag.Message = "Invalid Password!";
                        return View();
                    }
            }
            else
            {
                ViewBag.Message = "Invalid Email!";
                return View();
            }
© www.soinside.com 2019 - 2024. All rights reserved.