C# Programming: September 2016

Thursday, September 15, 2016

Pagination in ASP.NET MVC | Numbers Assigned to Pages with Next and Pre...

Welcome to ASP.NET MVC Tutorial on How to make Paging. We are going to create a paging with help of PagedList.

At first you need to install pagedlist.mvc from package manager console with this code.
install-package pagedlist.mvc



Other Code are as follows:

 using PagedList;

-----------------Home Controller code----- ------

 public ActionResult Index(int? page)

        {

            StudentContext Db = new StudentContext();//create object of context

            List<Student> students = new List<Student>();

            students = Db.Students.ToList();

            int pageSize = 3;

            int pageNumber = (page ?? 1);

             return View(students.ToPagedList(pageNumber, pageSize));

         

        }

--------------------------------------------------------

-------------View Code--------Index.cshtml---------------



@*@model IEnumerable<ASPtutorial.Models.Student>*@

@using PagedList.Mvc;

@model PagedList.IPagedList<ASPtutorial.Models.Student>



@{

    ViewBag.Title = "Index";

}



<h2>Index</h2>



<p>

    @Html.ActionLink("Create New", "Create")

</p>

<table class="table">

    <tr>

        <th>

           Student Name

        </th>

        <th>

       Address

        </th>

        <th>

            Phone

        </th>

        <th></th>

    </tr>



@foreach (var item in Model) {

    <tr>

        <td>

            @Html.DisplayFor(modelItem => item.sName)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.sAddress)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.sPhone)

        </td>

        <td>

            @Html.ActionLink("Edit", "Edit", new { id=item.sId }) |

            @Html.ActionLink("Details", "Details", new { id=item.sId }) |

            @Html.ActionLink("Delete", "Delete", new { id=item.sId })

        </td>

    </tr>

}



</table>

<br />

<div style="margin:5px;">

    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount



    @Html.PagedListPager(Model, page => Url.Action("Index",

    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

</div>



If you have still Doubt then watch this video to be more clear. For onlice classes contact in facebook.

fb.com/ythecoder



Tuesday, September 13, 2016

Difference Between Viewbag, ViewData and TempData in ASP.NET MVC | ASP Hero

Welcome to ASP.NET tutorial on Viewbag, ViewData and Tempdata.

Viewbag:

for sending data from controller to view, data destroys on redirection.

eg. ViewBag.message="something";



ViewData:

is also for sending data from controller to view, data destroys on redirection.

eg. ViewData["message"]="something";



TempData:

is also for sending data from controller to view, data does not destroys on redirection.

After one redirection data can be used and then data destroys.

eg. TempData["message"]="something";

For further details watch this video.
For online classes knock me at fb.com/ythecoder

Sunday, September 11, 2016

17.How to Make Session in ASP.NET MVC | Register, Login , Authentication...

Welcome to ASP.NET tutorial on How to make Session. please follow this video for solving your problem on session.

15. Authentication System in ASP.NET MVC | Register, Login , Authenticat...

Welcome to ASP.NET MVC Tutorial on how to make authentication system in asp.net mvc. The details are in this video.

15 How to make Login System in ASP.NET MVC | Register, Login , Authentic...

Welcome to ASP.NET MVC Tutorial on How to make Login System.
Just Put this code in home Controller.

  public ActionResult login()

        {

            return View();

        }



        [HttpPost]

        public ActionResult login(user user)

        {

            StudentContext db = new StudentContext();

         

            var userLoggedIn = db.users.SingleOrDefault(x => x.username == user.username && x.password == user.password);



            if (userLoggedIn != null)

            {

                ViewBag.message = "loggedin";

                ViewBag.triedOnce = "yes";

                return RedirectToAction("mainpage", "home",new { username= user.username });

            }

            else

            {

                ViewBag.triedOnce = "yes";

                return View();

            }





        }
And Paste this code in Home Folder's Login.cshtml

@model ASPtutorial.Models.user



@{

    ViewBag.Title = "login";

}



<h2>login</h2>



@using (Html.BeginForm())

{

    @Html.AntiForgeryToken()



    if (ViewBag.message == null && ViewBag.triedOnce == "yes")

    {

        <h4>Username or Password not matched, please log in again or register.@Html.ActionLink("Register", "Register")</h4>

    }



    <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.PasswordFor(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="Login" class="btn btn-default" />

            </div>

        </div>

    </div>

}



<div>

    @Html.ActionLink("Back to List", "Index")

</div>


For more questions or queries ask me on fb.com/ythecoder or you can watch this video too. If you are interested in online class then do contact me in affordable price. :)

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. :)




Thursday, September 8, 2016

Wednesday, September 7, 2016

Tuesday, September 6, 2016