C# Programming: 14. Registration of User in ASP.NET MVC | Register, Login , Authenticati...

Sunday, September 11, 2016

14. Registration of User in ASP.NET MVC | Register, Login , Authenticati...

Welcome to ASP.NET MVC tutorial on how to make a user registration.

I believe in less talking and more doing lets move on to writing code.
Lets make a student class i.e a model in model folder.

[Table("Students")]

    public class Student

    {

        [Key]

        public int sId { get; set; }

        [DisplayName("Student Name")]  //for display name above line must be added

        public string sName { get; set; } //just storing student Id,name,address,phone

        [DisplayName("Student Address")]

        public string sAddress { get; set; }

        [DisplayName("Student Phone")]

        public string sPhone { get; set; }

    }

lets create a context in model folder. I have given name student you can give anything you want

  public class StudentContext : DbContext

    {

   

        public DbSet<user> users { get; set; } //this line will get students info

    }



Put this code in home controller.

 public ActionResult register()

        {

            return View();

        }



Make respective view with scaffolding i.e register.cshtml with create template,student model, and studentContext or you can directly paste this code i dont mind :D


@model ASPtutorial.Models.user



@{

    ViewBag.Title = "register";

}



<h2>register</h2>



@using (Html.BeginForm())

{

    @Html.AntiForgeryToken()

   

    if(ViewBag.message!=null)

    {

        <h1>@ViewBag.message</h1>

    }



    <div class="form-horizontal">

        <h4>user</h4>

        <hr />

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <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>


Again in home controller put this code.
 [HttpPost]
        public ActionResult register(user user)
        {
            StudentContext db = new StudentContext();
            db.users.Add(user);
            db.SaveChanges();

            ViewBag.message = "You are successfully registered";

            return View();
        }
For futher description of each code you can watch this video too. :)




No comments:

Post a Comment