Hello Friends, I save and also update data from my controller to the Table directly and everything work fine befor I decided to pass the data from controller through view model to the database, but after the changes the data Fails to save and update.
HTML PAGE
function saveCategory() {
var model = new Object();
model.CategoryId = $("#categoryId").val().trim();
model.CategoryName = $("#categoryName").val().trim();
model.Status = $("#status").val().trim();
var data = JSON.stringify({
model: model
})
return $.ajax({
contentType: 'application/json; charset=ut-8',
dataType: 'json',
type: 'POST',
url: "/Home/SaveCategory",
data: data,
success: function (result) {
if (result == true) {
dataTable.ajax.reload();
}else {
swal("Save Failed!")
}
},
error: function () {
swal("Error!")
}
});
}
Emmmanuel FIADUFEPosted May 2, 2023, 11:38 PM
Thank you Mr Paul for your response, I will repost the error message soon. Thank you, am so much grateful.
Tuhin PaulPosted May 1, 2023, 6:28 PM
In your SaveCategory and Update actions, you are checking if ModelState.IsValid before saving or updating the data. Make sure that the validation rules for your view model are correct and that the data being passed from the view is valid. In your JavaScript, you have an error handling function that displays an error message if the AJAX request fails. Make sure that this function is displaying any error messages that are being returned by the server. Also check that your database is properly configured and that the connection string is correctly pointing to your database. Check that you have the necessary permissions to access the database and that there are no network connectivity issues.
Tuhin PaulPosted May 1, 2023, 6:26 PM
If you have introduced a view model between your controller and your database, make sure that the data is correctly mapped between the view model and the database model. Check that the property names match and that the view model is correctly passing the data to the controller action. Also one more thing is in your SaveCategory and Update actions, you are using Entity Framework to save or update data. Make sure that your database context (db) is correctly configured and that it is saving the data to the correct table.
Tuhin PaulPosted May 1, 2023, 6:11 PM
It is difficult to say what is causing the issue without more information such as error messages. Can you post any error message?
Emmmanuel FIADUFEPosted Apr 30, 2023, 4:38 AM
Sorry for the typo error.
model class I mean
Emmmanuel FIADUFEPosted Apr 29, 2023, 3:50 PM
HOMECONTROLLER
[HttpPost]
public ActionResult SaveCategory(CategoryModel model)
{
if(ModelState.IsValid)
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
tblCategory cat = new tblCategory();
//db.Entry(model).State = EntityState.Modified;
cat.Status = 1;
cat.CategoryName = model.CategoryName;
db.SaveChanges();
TempData["message"] = "Data Save Successfully";
}
else
{
TempData["message"] = "Validation Error";
}
return RedirectToAction("Category");
}
[HttpPost]
public ActionResult Update(CategoryModel model)
{
if(ModelState.IsValid)
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
tblCategory cat = db.tblCategory.Where(x=>x.Status==1 && x.CategoryId== CategoryId).FirstOrDefault();
cat.CategoryName = model.CategoryName;
db.Entry(cat ).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
TempData["message"] = "Data Updated Successfully";
}
else
{
TempData["message"] = "Validation Error";
}
return RedirectToAction("Category");
}