
model
{
public int VehicleID { get; set; }
public int VehicleTypeID { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public int Capacity { get; set; } = 0;
public bool IsActive { get; set; }
}
controller
// GET: Vehicle
[AccessRights(AccessKey = "VE")]
public ActionResult Vehicle()
{
DataSet tempDS = new DataSet();
string __ConnectionString = ConfigurationManager.ConnectionStrings["MySqlConnector"].ConnectionString;
tempDS = SelectDataSet(__ConnectionString, "SELECT * FROM vehicle WHERE IsActive = 1");
var ds = tempDS;
ViewBag.dataSource = ds.Tables[0];
ViewBag.Search = true;
return View();
}
[AccessRights(AccessKey = "VE")]
public ActionResult VehicleInsert(VehicleModel model)
{
try
{
string baseSql = "INSERT INTO `vehicle` (`Brand`, `Model`, `IsActive`) VALUES (@brand, @model, @isActive);";
Dictionary<string, object> param = new Dictionary<string, object>
{
{ "@brand", model.Brand },
{ "@model", model.Model },
{ "@isActive", model.IsActive }
};
DB2.ExecuteNonQueryEscape(baseSql, param);
}
catch (Exception ex)
{
return Json(new { success = false, error = ex.Message });
}
return Json(new { success = true });
}
[HttpGet]
[AccessRights(AccessKey = "VE")]
public ActionResult EditVehicle(int ID)
{
DataSet tempDS = new DataSet();
string __ConnectionString = ConfigurationManager.ConnectionStrings["MySqlConnector"].ConnectionString;
string query = $"SELECT * FROM vehicle WHERE VehicleID=" + ID + ";";
tempDS = SelectDataSet(__ConnectionString, query);
var ds = tempDS;
VehicleModel model = new VehicleModel();
foreach (DataRow row in ds.Tables[0].Rows)
{
model = (DBObjectMapper.Map<VehicleModel>(row));
}
return PartialView(model);
}
[AccessRights(AccessKey = "VE")]
public ActionResult VehicleUpdate(VehicleModel model)
{
try
{
if (model.VehicleID != 0)
{
string baseSql = "UPDATE `vehicle` SET `Brand` = @brand, `Model` = @model, `IsActive` = @isActive WHERE `VehicleID` = @vehicleId";
Dictionary<string, object> param = new Dictionary<string, object>
{
{ "@brand", model.Brand },
{ "@model", model.Model },
{ "@isActive", model.IsActive }
};
DB2.ExecuteNonQueryEscape(baseSql, param);
}
else
{
return Json(new { success = false, error = "ID cannot be null" });
}
}
catch (Exception ex)
{
return Json(new { success = false, error = ex.Message });
}
return Json(new { success = true });
}
[AccessRights(AccessKey = "VE")]
public ActionResult VehicleDelete(VehicleModel model)
{
try
{
if (model.VehicleID != 0)
{
string baseSql = "UPDATE `vehicle` SET `IsActive` = @isActive WHERE `VehicleID` = @vehicleId;";
Dictionary<string, object> param = new Dictionary<string, object>()
{
{ "@vehicleId", model.VehicleID },
{ "@isActive", 0 },
};
DB2.ExecuteQueryScalarEscape(baseSql, param);
}
else
{
return Json(new { success = false, error = "ID cannot be null" });
}
}
catch (Exception ex)
{
return Json(new { success = false, error = ex.Message });
}
return Json(new { success = true });
}