Professional Documents
Culture Documents
Understanding Form Validation Validation Using Dataannotations in Mvc5 Custom Validations Using Data Annotation Fetching Data From Database
Understanding Form Validation Validation Using Dataannotations in Mvc5 Custom Validations Using Data Annotation Fetching Data From Database
Understanding Form Validation Validation Using Dataannotations in Mvc5 Custom Validations Using Data Annotation Fetching Data From Database
Video Links:
https://www.youtube.com/watch?v=NrKyURTs45Y&list=PLjVp2I_4DPy_4CvU-
mdB_vfHfT8k7o18N&index=11&t=1270s
https://www.youtube.com/watch?v=98woEi4mPOs&list=PLjVp2I_4DPy_4CvU-
mdB_vfHfT8k7o18N&index=12&t=29s
https://www.youtube.com/watch?v=RJpFTkKHiBA&list=PLjVp2I_4DPy_4CvU-
mdB_vfHfT8k7o18N&index=13&t=45s
https://www.youtube.com/watch?v=LorEsTcvzqE&list=PLjVp2I_4DPy_4CvU-
mdB_vfHfT8k7o18N&index=14&t=663s
First of all create a class in Model Folder with name Employee. Here is the code for
Employee class.
using MVCDemo.CustomValidation;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MVCDemo.Models
{
public class Employee
{
public int EmployeeID { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
[Required]
public string Gender { get; set; }
[Required]
[EmailAddress]
public string EmailAddress { get; set; }
public string City { get; set; }
[Display(Name ="Salary")]
public int Salary { get; set; }
[Required]
[Display(Name = "Education Level")]
public int EducationLevel { get; set; }
}
}
In the employee model class we have decorated different properties with attributes. Some have
one annotation as FirstName is required only. While some has multiple attributes such as age is
required and range must be between 20 and 60.
Pay special attention to highlight attribute CustomHireDate. It is user defined attribute to
perform custom validation. We have to make sure that Date must not greater than today using
this attribute. To create this custom attribute for HireDate follow these steps.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MVCDemo.CustomValidation
{
public class CustomHireDate : ValidationAttribute
{
public override bool IsValid(object value)
{
DateTime dateTime = Convert.ToDateTime(value);
Read the above code carefully. CustomHireDate must be child class of Validation Attribute
class. Than we have to override IsValid method of ValidationAttribute class. In this overridden
method we can provide our own logic for validation.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MVCDemo.Models
{
public class Department
{
};
}
}
}
using MVCDemo.DAL;
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCDemo.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Create()
{
ViewBag.DepartmentsList = getDepartmentList();
return View();
}
[HttpPost]
public ActionResult Create(Employee emp)
{
/*
1.ModelState is a property of controller class that helps
to validate form data in server side.
2. If any of the model fields are not matching with their
defined type(Employee Model Class in our class), then
ModelState.IsValid will return false.
*/
if (ModelState.IsValid) {
//Code to insert data
}
ViewBag.DepartmentsList = getDepartmentList();
return View();
}
@model MVCDemo.Models.Employee
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<title>Employee Information</title>
</head>
<body>
@using (Html.BeginForm("Create","Employee"))
{
<div class="h4">
@Html.ActionLink("Back to Main Page", "Index")
</div>
<div class="form-horizontal">
<h4 class="h1 text-center">Employee Information</h4>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName,
"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName,
"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new
{ @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Gender, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Gender, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailAddress, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model =>
model.EmailAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Salary, htmlAttributes: new
{ @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Salary, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Salary, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Age, "", new
{ @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EducationLevel,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EducationLevel, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model =>
model.EducationLevel, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HireData, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HireData, new {
htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HireData,
"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Department.DepartmentID,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.Department.DepartmentID,
(List<SelectListItem>)ViewBag.DepartmentsList)
</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>
}
</body>
</html>
In the Create view we have used bootstrap to beautify look and feel of UI. Html.LabelFor
helper automatically render the display name of model property on the view. This is just like
html label tag.
@Html.EditorFor generates input control just like @Html.TextBox but the key
difference is that Html.EditorFor helper method generates input control based on underlying
datatype specified in the model class. For example the data type of salary in Model
class so you can not specify alphabets here. Only number is allowed.
@Html.ActionLink is used navigate from on view to other. In this case it will render the
index view of current controller (Employee Controller).
Note: The given Data Control (For HireDate) will only work in Google Chrome or Edge
Browsers. In advance lectures we will discuss use of JQuery Date Picker to resolve this
issue.
Here we are using ExecuteReader Method to fetch data from database. Use of DataReader
class can enhance performance of application as it used connected approached and hold just
one row at a time.
Our program reads data rows one by one in a loop and store each column information in
relevant variable of model class object.
Now we will have an example code to clarify these all concepts. Create two tables Employee
and Department in database as described in the following screenshots.
Update Employee Entity class as following.
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace MVCDemo.DAL
{
public class EmployeeEntity
{
string ConnectionString = @"data source=DESKTOP-
E28E3Q2\SQLEXPRESS;initial catalog=EMS;integrated security=True";
SqlConnection sqlConnection = null;
SqlCommand cmd = null;
public int insert(Employee employee)
{
int effectedRows = 0;
try
{
sqlConnection = new SqlConnection(ConnectionString);
string query = @"insert into
Employee(FirstName,LastName,Gender,Age,EducationLevel,Salary,EmailAddr
ess,HireDate,City,DepartmentId)
values('" + employee.FirstName + "','"
+ employee.LastName + "','" + employee.Gender + "','" + employee.Age +
"','" + employee.EducationLevel + "','" + employee.Salary + "','" +
employee.EmailAddress + "','" + employee.HireData + "','"+ employee
.City+ "','" + employee.Department.DepartmentID + "')";
sqlConnection.Open();
cmd = new SqlCommand(query, sqlConnection);
effectedRows = cmd.ExecuteNonQuery();
sqlConnection.Close();
return effectedRows;
}
catch (Exception exp)
{
return effectedRows;
}
}
sqlConnection.Open();
cmd = new SqlCommand(query, sqlConnection);
SqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
EmployeeList.Add(new Employee
{
FirstName = dataReader["FirstName"].ToString(),
LastName = dataReader["LastName"].ToString(),
Gender = dataReader["Gender"].ToString(),
EmailAddress =
dataReader["EmailAddress"].ToString(),
City = dataReader["City"].ToString(),
Salary
=Convert.ToInt32(dataReader["Salary"].ToString()),
Age=
Convert.ToInt32(dataReader["Age"].ToString()),
EducationLevel =
Convert.ToInt32(dataReader["EducationLevel"].ToString()),
Department=new Department {
DepartmentID=
Convert.ToInt32(dataReader["DepartmentID"].ToString()),
Name = dataReader["Name"].ToString()
},
HireData=
DateTime.Parse(dataReader["HireDate"].ToString())
});
}
sqlConnection.Close();
return EmployeeList;
}
}
}
Create a new Class Department Entity in DAL Folder and paste following code.
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace MVCDemo.DAL
{
public class DepartmentEntity
{
string ConnectionString = @"data source=DESKTOP-
E28E3Q2\SQLEXPRESS;initial catalog=EMS;integrated security=True";
SqlConnection sqlConnection = null;
SqlCommand cmd = null;
while (dataReader.Read())
{
DepartmentList.Add(new Department
{
DepartmentID =
Convert.ToInt32(dataReader["DepartmentID"].ToString()),
Name = dataReader["Name"].ToString()
});
}
sqlConnection.Close();
return DepartmentList;
}
}
}
using MVCDemo.DAL;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MVCDemo.Models
{
public class Department
{
using MVCDemo.DAL;
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCDemo.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
Add Index view in Employee Folder to show all employees. The code for Index view is as
following.
@model IEnumerable<MVCDemo.Models.Employee>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Employee List</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table" border="1">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailAddress)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.EducationLevel)
</th>
<th>
@Html.DisplayNameFor(model => model.HireData)
</th>
<th>
@Html.DisplayNameFor(model => model.Department.Name)
</th>
<th></th>
</tr>
@if (Model != null && Model.Count() > 0)
{
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.EducationLevel)
</td>
<td>
@Html.DisplayFor(modelItem => item.HireData)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.Name)
</td>
</tr>
}
}
else
{
<tr>
<td colspan="10">
No Record Found
</td>
</tr>
}
</table>
</body>
</html>
Assignment #5
Dear students read the given lectures carefully as per the course objectives mentioned on the top
and carryout the assignment as per following instructions
1. Submission Date: Sunday 12-April-2020 at 11:59 PM. This will also count as your
Attendance for this week.
2. You must prepare handwritten Assignment with implementation in the form of project.
3. Send it to respective course teacher (after scanning it) for assessment by email only.
Part 1: Create a Form to save Mobile Data in database. Apply specified validation within
parenthesis using concepts provided in this lecture. It must have following fields:
1: IMEI Number 2: Operating System (android as well as iOS and populate dropdown from database
table named OS) 3: Released Date (Must not greater than today)
4: Color (length must be great than 2 and less than 15) 5: Weight in grams
6: Model Number 7: Ram (Must be greater than 1 GB and less than 8 GB)
8: Price (Must be greater than 10,000 and less than 20, 0000)
Part 2: Create another view to display all saved information (in SQL server DB) in Mobile Data
using Table Tag.