I have only one table for login and registration.
problem is login is not working as well as registration is not work
stanstud.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; } } context class
public class stanstudcontext:DbContext { public stanstudcontext():base("cnnos") { } public DbSet studs { get; set; } }
controller
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"; } Loginh2> Demo Websitetitle> head> @using (Html.BeginForm()) { UserName:label> @Html.EditorFor(model => model.username) div> Password:label> @Html.EditorFor(model => model.password) div> <input type="submit" value="Login" /> div> } body> html>Regis.cshtml @{ ViewBag.Title = "Regis"; } Regish2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> stanstudsh4> <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> } @Html.ActionLink("Back to List", "Index") div>when user registration then insert by default entry as a user my registration is not working when I press register button then the record is not inserted and when I press login then login is not working? something is wrong my script?? please help??
Demo Websitetitle>
head>
@using (Html.BeginForm()){UserName:label>@Html.EditorFor(model => model.username)div>Password:label>@Html.EditorFor(model => model.password)div><input type="submit" value="Login" />div>}body>
html> Regis.cshtml
Regis.cshtml
@{ ViewBag.Title = "Regis"; } Regish2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> stanstudsh4> <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> } @Html.ActionLink("Back to List", "Index") div> when user registration then insert by default entry as a user my registration is not working when I press register button then the record is not inserted and when I press login then login is not working? something is wrong my script?? please help??
8 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted Dec 16, 2019, 3:05 AM
Rahul PatilPosted Dec 16, 2019, 1:48 AM
Rahul PatilPosted Dec 16, 2019, 1:41 AM
Amit MohantyPosted Dec 16, 2019, 12:23 AM
Salman BegPosted Dec 15, 2019, 11:40 PM
Rahul PatilPosted Dec 15, 2019, 11:32 PM
Ahmed AbdiPosted Dec 15, 2019, 11:27 PM
Sanwar RanwaPosted Dec 15, 2019, 11:23 PM