Skip to main content

SignIn_SignUp in Asp.net Mvc

Models:

SignUp Class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Signin_Signup.Models
{
    [Table("tbl_account")]
    public class SignUp
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public string password { get; set; }
    }

}



DB Class:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Signin_Signup.Models
{
    public class db:DbContext
    {
        public DbSet<SignUp> signups { get; set; }
    }

}


User Controller:

using Signin_Signup.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Signin_Signup.Controllers
{
    public class UserController : Controller
    {
        db obj = new db();
        // GET: User
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Signup()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Signup(SignUp model)
        {
            SignUp s = new SignUp();
            s.name = model.name;
            s.email = model.email;
            s.password = model.password;
           
                obj.signups.Add(s);
                obj.SaveChanges();
            
           
           
            return RedirectToAction("Signin");
        }
        public ActionResult Signin()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Signin(SignUp model)
        {
            SignUp s = obj.signups.Where(x => x.email.Equals(model.email) && x.password.Equals(model.password)).SingleOrDefault();
            if (s!=null)
            {
                return RedirectToAction("UserDashboard");
            }
            else
            {
                ViewBag.msg = "Invalid Email/Password";
            }
            return View();
        }
        public ActionResult UserDashboard()
        {
            return View();
        }
    }

}



Views:

SignIn.cshtml:

@model Signin_Signup.Models.SignUp

@{
    ViewBag.Title = "Signin";
}

<h2>Signin</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
     
       
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.email, "", 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>
   @ViewBag.msg
}

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

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>

<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>






SignUp.cshtml:

@model Signin_Signup.Models.SignUp

@{
    ViewBag.Title = "Signup";
}

<h2>Signup</h2>


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

        <div class="form-group">
            @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.email, "", 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>
}
@ViewBag.msg1
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>



UserDashboard.cshtml:


@{
    ViewBag.Title = "UserDashboard";
}

<h2>UserDashboard</h2>


Then Make a Connection with your database and after successfully make connection , just add this line to your Web.config file:

<add name="dbContext" connectionString="Data Source=DESKTOP-UTTQQQ1;Initial Catalog=LockDownExample;Integrated Security=True" providerName="System.Data.SqlClient" />



Note:
Name = "Could be anything"
connectionString = "database connection"

Comments

Popular posts from this blog

CRUD Operation in ASP.NET MVC Using ADO.NET Jquery Ajax

In this article of ASP.NET MVC we will learn about CRUD operation using jquery ajax so let's start. Firstly open visual studio: Go to File => New and Select Project,when you select project then New Project Dialogbox will appear. Select ASP.NET Web Application (.NET Framework) and give a name to your project , as we are building CRUD Operation so our project's name could be CRUD_OPEATION and then select OK. Now you will see a new dialogbox, select Empty Template and MVC folder. After create project you will see a like screen like this: Now go to Models folder and add ADO.NET Entity Data Model and give any name like in my case i give dbEntities and selec OK: After that you will see a dilogbox called Entity Data Model Wizard, select EF Designer from database and select Next: After that a new dilogbox will appear and it will ask a new connection to your sql server, then select New Connection and give server name ( if you want to see your server name then open sql server,it will ...

Showing List of database Using Jquery, Ajax and Datatable

In this article of ASP.NET MVC we will learn about Showing List of database Using Jquery, Ajax and Datatable so let's start: Firstly create a model class which you want to show a list of datatable.In my case i create a model class namely called GEN_TaskMaster:  public class GEN_TaskMaster     {         public long sysTaskID { get; set; }         public long sysUserID { get; set; }                  public DateTime Task_Date { get; set; }         public string Task_Assign_To { get; set; }         public string Task_Assign_From { get; set; }         public string Task_Description { get; set; }                 } Then go to your controller and paste a code below: public ActionResult ListAll()         {             var list = db.Database...

ASP.NET MVC - Get Started

ASP.NET is a free web framework for building websites and web applications on .NET Framework using HTML, CSS, and JavaScript. These tutorials are designed for beginners and professionals who want to learn ASP.NET MVC 5. In the next article we will learn How to Create First Asp.net MVC application. Click Here To Create First MVC Application