Change the "model" to something else like "vm". The name "model" is causing a naming conflict with the property named "model"
public ActionResult VehicleInsert(VehicleModel vm)
{
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 });
}
Change the "model" to something else like "vm". The name "model" is causing a naming conflict with the property named "model"
public ActionResult VehicleInsert(VehicleModel vm)
{
Use Fiddler to see if the data are properly sent from client to server.
Use debugger of Visual Studio to see if the data sent from client are properly bound to the model
of the action method VehicleInsert(VehicleModel model)
.