This example for add and search record using entity framework in MVC3 using JSON.
Entity frame work database design is given below:
http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/creating-model-classes-with-the-entity-framework-cs
HomeController Page
Model-> SelectViewModel
Model-> VehicleCategoryModel
Model-> VehicleFacilityModel
Model-> VehicleRentTypeModel
Home->Index
Home->About
Entity frame work database design is given below:
http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/creating-model-classes-with-the-entity-framework-cs
HomeController Page
namespace MvcAppVehicleSystem.Controllers { public class HomeController : Controller { VehicleSysHireEntities2 _db; public HomeController() { _db = new VehicleSysHireEntities2(); } public ActionResult Index() { var query = _db.VehicleRentTypes.ToList(); var query2 = _db.VehicleCategories.ToList(); var model = new Models.SelectViewModel { CategoryList = query2, RentTypeList = query }; return View(model); } [HttpPost] public ActionResult Index(VehicleMasterModel role) { VehicleMaster currentVehicle = new VehicleMaster(); currentVehicle.Brand = role.Brand; currentVehicle.Category = role.Category; currentVehicle.Model = role.Model; currentVehicle.Color = role.Color; currentVehicle.NoOfYear = role.NoOfYear; currentVehicle.RentType = role.RentType; _db.AddToVehicleMasters(currentVehicle); _db.SaveChanges(); if (currentVehicle.ID > 0) { VehicleFacility currentfacility = new VehicleFacility(); currentfacility.VehicleMasterID = currentVehicle.ID; currentfacility.HasABS = role.HasABS; currentfacility.HasAirCon = role.HasAirCon; currentfacility.HasCTX = role.HasCTX; _db.AddToVehicleFacilities(currentfacility); _db.SaveChanges(); } return Json(true); } public ActionResult About() { var query = _db.VehicleRentTypes.ToList(); var query2 = _db.VehicleCategories.ToList(); var model = new Models.SelectViewModel { CategoryList = query2, RentTypeList = query }; return View(model); } [HttpPost] public ActionResult About(VehicleMaster role) { ListobjList = (from m in _db.VehicleMasters select m).ToList(); return Json(objList); } [HttpPost] public ActionResult AboutResult(VehicleMasterModel role) { var objList = (from m in _db.VehicleMasters where m.RentType == role.RentType select new VehicleMasterModel() { Brand = m.Brand, Model = m.Model }).ToList(); return Json(objList, JsonRequestBehavior.AllowGet); } } }
Model-> SelectViewModel
public class SelectViewModel { public ListCategoryList { get; set; } public List RentTypeList { get; set; } public VehicleFacility currentvehiclefacility { get; set; } public VehicleMaster currentvehiclemaster { get; set; } }
Model-> VehicleCategoryModel
public class VehicleCategoryModel { public int ID { get; set; } public string Category { get; set; } public ListVehicleCategoryList { get; set; } }
Model-> VehicleFacilityModel
public class VehicleFacilityModel { public ListVehicleMasterList { get; set; } public int ID { get; set; } public bool HasAirCon { get; set; } public bool HasABS { get; set; } public bool HasCTX { get; set; } public int VehicleMasterID { get; set; } }
Model-> VehicleMasterModel
public class VehicleMasterModel { public int ID { get; set; } public int Category { get; set; } public string Brand { get; set; } public string Model { get; set; } public string Color { get; set; } public int NoOfYear { get; set; } public int RentType { get; set; } public bool HasAirCon { get; set; } public bool HasABS { get; set; } public bool HasCTX { get; set; } public int VehicleMasterID { get; set; } }
Model-> VehicleRentTypeModel
public class VehicleRentTypeModel { public int ID { get; set; } public string RentType { get; set; } public ListVehicleRetTypeList { get; set; } }
Home->Index
@{ ViewBag.Title = "Home Page"; }@ViewBag.Message
@model MvcAppVehicleSystem.Models.SelectViewModel
Type @Html.DropDownList("ddlType", new SelectList(Model.RentTypeList, "ID", "RentType"), "Select Type", new { required = "required" }) Category @Html.DropDownList("ddlCategory", new SelectList(Model.CategoryList, "ID", "Category"), "Select Category", new { required = "required" }) Model @Html.DropDownList("ddlModel", new[] { new SelectListItem() {Text = "Audi",Value = "Audi"}, new SelectListItem() {Text = "Maruti",Value = "Maruti" }, new SelectListItem() {Text = "Suzuki",Value = "Suzuki" } }, "Choose model", new { required = "required" }) No of year @Html.DropDownList("ddlNoofYear", new[] { new SelectListItem() {Text = "2010",Value = "2010"}, new SelectListItem() {Text = "2011",Value = "2 011" }, new SelectListItem() {Text = "2012",Value = "2012" }, new SelectListItem() {Text = "2013",Value = "2013" } }, "Choose year", new { required = "required" }) Brand Has AirCon Has ABS Has CTX Colour
Home->About
@{ ViewBag.Title = "Search"; } @model MvcAppVehicleSystem.Models.SelectViewModelSearch
Type @Html.DropDownList("ddlType", new SelectList(Model.RentTypeList, "ID", "RentType"), "Select Type") No of year @Html.DropDownList("ddlNoofYear", new[] { new SelectListItem() {Text = "2010",Value = "2010"}, new SelectListItem() {Text = "2011",Value = "2011" }, new SelectListItem() {Text = "2012",Value = "2012" }, new SelectListItem() {Text = "2013",Value = "2013" } }, "Choose year")
No comments:
Post a Comment