You are on page 1of 4

public class CustomerController : Controller

{
// GET: Customer
public ActionResult Load() // connect via browser HTML
{
Customer obj =
new Customer
{
CustomerCode = "1001",
CustomerName = "Shiv"
};
if (Request.QueryString["Type"] == "HTML")
{
return View("Customer", obj);
}
else
{
return Json(obj, JsonRequestBehavior.AllowGet);
}
}
public ActionResult Enter()
{
// view model object
CustomerViewModel obj = new CustomerViewModel();
// // single object is fresh
obj.customer = new Customer();
// dal i have filled the customers collection
return View("EnterCustomer", obj);
}
public ActionResult EnterSearch()
{
CustomerViewModel obj = new CustomerViewModel();
obj.customers = new List<Customer>();
return View("SearchCustomer", obj);
}
public ActionResult getCustomers() // JSON collection
{
CustomerDal dal = new CustomerDal();
List<Customer> customerscoll = dal.Customers.ToList<Customer>();
Thread.Sleep(10000);
return Json(customerscoll, JsonRequestBehavior.AllowGet);
}
public ActionResult SearchCustomer()
{
CustomerViewModel obj = new CustomerViewModel();
CustomerDal dal = new CustomerDal();
string str = Request.Form["txtCustomerName"].ToString();
List<Customer> customerscoll
= (from x in dal.Customers
where x.CustomerName == str
select x).ToList<Customer>();
obj.customers = customerscoll;
return View("SearchCustomer", obj);
}
public ActionResult Submit() // validation runs
{

Customer obj = new Customer();


obj.CustomerName = Request.Form["Customer.CustomerName"];
obj.CustomerCode = Request.Form["Customer.CustomerCode"];
if(ModelState.IsValid)
{
// insert the customer object to database
// EF DAL
CustomerDal Dal = new CustomerDal();
Dal.Customers.Add(obj); // in mmemory
Dal.SaveChanges(); // physical commit
}
CustomerDal dal = new CustomerDal();
List<Customer> customerscoll = dal.Customers.ToList<Customer>();
return Json(customerscoll, JsonRequestBehavior.AllowGet);

}
Dal
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using HelloWorld.Models;
namespace HelloWorld.Dal
{
public class CustomerDal : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>().ToTable("tblCustomer");
}
public DbSet<Customer> Customers { get; set; }
}
}
Model
namespace HelloWorld.Models
{
public class Customer
{
[Required]
[RegularExpression("^[A-Z]{3,3}[0-9]{4,4}$")]
[Key]
public string CustomerCode { get; set; }

}
}

[Required]
[StringLength(10)]
public string CustomerName { get; set; }

viewModel
namespace HelloWorld.ViewModel
{
public class CustomerViewModel
{
// Customer
public Customer customer { get; set; }
// List of customers
public List<Customer> customers { get; set; }
}
}
EnterCustomer.cshtml
@model HelloWorld.ViewModel.CustomerViewModel
@using HelloWorld.Models;
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>EnterCustomer</title>
</head>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<body>
<div>
<form id="frm1">
<i>Customer Name</i> @Html.TextBoxFor(m => m.customer.CustomerName) <br />
@Html.ValidationMessageFor(x => x.customer.CustomerName)<br />
<i> Customer Code </i> @Html.TextBoxFor(m => m.customer.CustomerCode)<br />
@Html.ValidationMessageFor(x => x.customer.CustomerCode)<br />
</form>
<input id="Btn" type="button" value="Add customer" onclick="SendData()" />
<br />
<div id="status"></div>
<table id="tbl">
<tr>
<td>Customer Code</td>
<td>Customer Name</td>
</tr>
</table>
<script language="javascript">
$("#status").text("Loading...");
$.get("getCustomers", null, BindData);
function BindData(Customers)
{
var tbl = $("#tbl"); // got reference of the table
// For loop
for (var j = 0; j < Customers.length ; j++)
{
var newRow = "<tr>" +
"<td>" + Customers[j].CustomerCode + "</td>" +

"<td>" + Customers[j].CustomerName + "</td>" +


"</tr>";
tbl.append(newRow);
}
$("#status").text("");
}
function SendData()
{
// post call to the MVC controller
// reference of the form , serialize
$("#status").text("Adding please wait....");
var frm = $("#frm1").serialize();
$.post("Submit", frm, BindData);
// set the customer code and name textbox
// nothing
$("#customer_CustomerName").val("");
$("#customer_CustomerCode").val("");
}
</script>
</div>
</body>
</html>

You might also like