C# asp.net mvc中的登录和注册问题?
我是一名学生,我正在学习MVC 我只有一个表用于登录和注册 问题是登录不起作用,注册也不起作用C# asp.net mvc中的登录和注册问题?,c#,asp.net,view,model,controller,C#,Asp.net,View,Model,Controller,我是一名学生,我正在学习MVC 我只有一个表用于登录和注册 问题是登录不起作用,注册也不起作用 stanstuds.cs public class stanstuds { [Key] public int id { get; set; } public string fname { get; set; } public string mname { get; set; } public str
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; }
}
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>
@model LoginRegOneTable.Models.stanstuds
@{
ViewBag.Title=“登录”;
}
登录
演示网站
@使用(Html.BeginForm())
{
用户名:
@EditorFor(model=>model.username)
密码:
@EditorFor(model=>model.password)
}
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>
@{
ViewBag.Title=“Regis”;
}
里吉斯
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
斯坦斯特尔斯
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.fname,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.fname,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.fname,“,new{@class=“text danger”})
@LabelFor(model=>model.mname,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.mname,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.mname,“,new{@class=“text danger”})
@LabelFor(model=>model.lname,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.lname,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.lname,“,new{@class=“text danger”})
@LabelFor(model=>model.country,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.country,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.country,“,new{@class=“text danger”})
**我认为问题在于,我想以用户身份默认插入条目,但如何插入?**
@LabelFor(model=>model.usertype,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.usertype,new{htmlAttributes=new{@class=“form control”,@value=“User”})
@Html.ValidationMessageFor(model=>model.usertype,“,new{@class=“text danger”})
@LabelFor(model=>model.username,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.username,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.username,“,new{@class=“text danger”})
@LabelFor(model=>model.password,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.password,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.password,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
当用户注册时,然后作为用户插入默认条目。当我按“注册”按钮时,我的注册不起作用;当我按“登录”按钮时,记录不插入;当我按“登录”按钮时,登录不起作用?我的剧本有点不对劲??请提供帮助???在这种情况下,您可以检查键入的密码是否等于数据库中的密码,并检查其是否有效:
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);
if (userdata.usertype == "admin")
return RedirectToAction("Index", "LogReg");
else
return RedirectToAction("Clientview", "LogReg");
}
else
{
ViewBag.Message = "Invalid Password!";
return View();
}
}
else
{
ViewBag.Message = "Invalid Email!";
return View();
}
在登录视图中,您并没有使用HTMLHelper,您只是使用了简单的标记,这不会有任何帮助。尝试使用@Html.TextBoxFor(m=>m.fname)和后续的Html帮助程序作为中间名和姓氏。然后看什么happens@RohanRaoif(userdata!=null)//当我调试时,此条件未被检查,我更改了登录名。cshtmlgive错误httpsessionstatebase不包含setint32的定义,并且没有扩展方法setnt32是u缺少指令或汇编引用???@dipsvirag使用Microsoft.AspNetCore.Http;对于.net core,如果不使用.net core,则可以按会话[“id”]=userdata.id创建会话;对于集合:……System.Web.HttpContext.Current.Session[“Id”]=userdata.Id;对于get:……var id=Convert.ToInt32(System.Web.HttpContext.Current.Session[“id]”);
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);
if (userdata.usertype == "admin")
return RedirectToAction("Index", "LogReg");
else
return RedirectToAction("Clientview", "LogReg");
}
else
{
ViewBag.Message = "Invalid Password!";
return View();
}
}
else
{
ViewBag.Message = "Invalid Email!";
return View();
}