In Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVCDemo.Entity;
namespace MVCDemo.Entity
{
public class Customer
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string Title { get; set; }
public string TitleofCourtesy { get; set; }
public string Language { get; set; }
public bool IsLanguage { get; set; }
public DateTime DOB { get; set; }
public DateTime DOJ { get; set; }
public double Salary { get; set; }
public string Email { get; set; }
}
}
In Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Entity;
using System.Text.RegularExpressions;
namespace MvcDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Customer()
{
ViewBag.Message = "Your Employee page.";
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem
{
Text = "Option1",
Value = "Option1"
});
items.Add(new SelectListItem
{
Text = "Option2",
Value = "Option2"
});
items.Add(new SelectListItem
{
Text = "Option3",
Value = "Option3"
});
items.Add(new SelectListItem
{
Text = "Option4",
Value = "Option4"
});
ViewBag.ListOfItems = items;
TempData["ListofItems"] = items;
return View();
}
[HttpPost]
public ActionResult Customer(Customer objCustomer, string radios, string[] departments)
{
ViewBag.Message = "Your Employee page.";
ViewBag.ListOfItems = TempData["ListofItems"];
TempData.Keep();
if (string.IsNullOrEmpty(objCustomer.LastName))
{
ModelState.AddModelError("LastName", "Last Name is required");
}
if (string.IsNullOrEmpty(objCustomer.FirstName))
{
ModelState.AddModelError("FirstName", "First Name is required");
}
if (objCustomer.Title == "0")
{
ModelState.AddModelError("Title", "Title is required");
}
if (string.IsNullOrEmpty(radios))
{
ModelState.AddModelError("TitleofCourtesy", "Title of Coutesy is required");
}
if (departments==null)
{
ModelState.AddModelError("Language", "Language is required");
}
if (!string.IsNullOrEmpty(objCustomer.Email))
{
string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(emailRegex);
if (!re.IsMatch(objCustomer.Email))
{
ModelState.AddModelError("Email", "Email is not valid");
}
}
else
{
ModelState.AddModelError("Email", "Email is required");
}
if (ModelState.IsValid)
{
// Perform Operations
}
return View();
}
}
}
In Views
@model MVCDemo.Entity.Customer
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Customer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<h3>Add Employee</h3>
<div class="container">
@using (Html.BeginForm("Customer","Home", FormMethod.Post)) {
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">Last Name</div>
<div class="col-md-3">
@Html.TextBoxFor(m => m.LastName, new { id = "txtLastName", @class = "form-control" })
</div>
<div class="col-md-6">
<div class="text-danger" id="errLastName">
@Html.ValidationMessageFor(m => m.LastName)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">First Name</div>
<div class="col-md-3">
@Html.TextBoxFor(m => m.FirstName, new { id = "txtFirstName", @class = "form-control" })
</div>
<div class="col-md-6">
<div class="text-danger" id="errFirstName">
@Html.ValidationMessageFor(m => m.FirstName)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">Title</div>
<div class="col-md-3">
@Html.DropDownListFor(m => m.Title,
new List<SelectListItem> {
new SelectListItem { Value = "0" , Text = "Select Title" },
new SelectListItem { Value = "1" , Text = "Option A" },
new SelectListItem { Value = "2" , Text = "Option B" },
new SelectListItem { Value = "3" , Text = "Option C" }
},
new { id = "drpTitle", @class = "form-control" })
</div>
<div class="col-md-6">
<div class="text-danger" id="errTitle">
@Html.ValidationMessageFor(m => m.Title)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">Title of Courtesy</div>
<div class="col-md-3">
@if (ViewBag.ListOfItems != null)
{
foreach (var c in ViewBag.ListOfItems)
{
<input type="radio" name="radios" value="@c.Value" />@c.Text
}
}
</div>
<div class="col-md-6">
<div class="text-danger" id="errTOC">
@Html.ValidationMessageFor(m => m.TitleofCourtesy)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">Language</div>
<div class="col-md-3">
@if (ViewBag.ListOfItems != null)
{
foreach (var c in ViewBag.ListOfItems)
{
<input type="checkbox" name="departments" value="@c.Value" />@c.Text
}
}
</div>
<div class="col-md-6">
<div class="text-danger" id="errLanguage">
@Html.ValidationMessageFor(m => m.Language)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">DOB</div>
<div class="col-md-3">
@Html.TextBoxFor(m => m.DOB, new { id = "txtDOB", @class = "form-control" })
</div>
<div class="col-md-6">
<div class="text-danger" id="errDOB">
@Html.ValidationMessageFor(m => m.DOB)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">DOJ</div>
<div class="col-md-3">
@Html.TextBoxFor(m => m.DOJ, new { id = "txtDOJ", @class = "form-control" })
</div>
<div class="col-md-6">
<div class="text-danger" id="errDOJ">
@Html.ValidationMessageFor(m => m.DOJ)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">Salary</div>
<div class="col-md-3">
@Html.TextBoxFor(m => m.Salary, new { id = "txtSalary", @class = "form-control" })
</div>
<div class="col-md-6">
<div class="text-danger hidden" id="errSalary">
@Html.ValidationMessageFor(m => m.Salary)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3">Email</div>
<div class="col-md-3">
@Html.TextBoxFor(m => m.Email, new { id = "txtEmail", @class = "form-control" })
</div>
<div class="col-md-6">
<div class="text-danger" id="errEmail">
@Html.ValidationMessageFor(m => m.Email)
</div>
</div>
</div>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-md-3"> </div>
<div class="col-md-3">
<input type="submit" id="btnSave" class="btn btn-primary" value="Save" />
</div>
<div class="col-md-6">
</div>
</div>
<div class="clear-fix"> </div>
}
</div>
</div>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function () {
$("#txtDOB").datepicker({ maxDate: '0' });
$("#txtDOJ").datepicker({ minDate: '0' });
$("#btnSave").click(function () {
});
});
</script>
</body>
</html>
No comments:
Post a Comment