Monday, October 17, 2016
Sunday, October 16, 2016
Difference between Single or SingleOrDefault in ASP.NET MVC LINQ with Da...
Welcome to ASP.NET tutorial on Single or SingleOrDefault. The exact difference is described in the video.
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
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";
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
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>
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. :)
Subscribe to:
Posts (Atom)