I am scratching my head with this one. My code appears correct but when I select in the view to add a row it is actually calling the edit in the controller. I know this because I get an error when I save. I changed the error message in my controller to verify that the message was coming from the edit code. Is there something I am not seeing here? thanks for your help.
What I have tried:
Jqgrid
{
zIndex: 100,
url: "/Admin/CreateCustomer",
closeOnEscape: true,
closeAfterAdd: true,
afterComplete: function (response) {
if (response.responseJSON) {
if (response.responseJSON === "Saved Successfully") {
alert("Saved Successfully");
}
else {
var message = "";
for (var i = 0; i < response.responseJSON.length; i++) {
message += response.responseJSON[i];
message += "\n";
}
}
}
}
},
{
zIndex: 100,
url: '/Admin/EditCustomer',
closeOnEscape: true,
closeAfterEdit: true,
recreateForm: true,
afterComplete: function (response) {
if (response.responseText) {
alert(response.responseText);
}
}
},
Controller
[HttpPost]
public JsonResult CreateCustomer([Bind(Exclude = "ID")] Customer customer)
{
StringBuilder msg = new StringBuilder();
try
{
if (ModelState.IsValid)
{
using (StoreEntities db = new StoreEntities())
{
db.CustomerSet.Add(customer);
db.SaveChanges();
return Json("Saved Successfully", JsonRequestBehavior.AllowGet);
}
}
else
{
var errorList = (from item in ModelState
where item.Value.Errors.Any()
select item.Value.Errors[0].ErrorMessage).ToList();
return Json(errorList, JsonRequestBehavior.AllowGet);
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
var errorList = new List<string>();
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorList.Add(String.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
}
}
return Json(errorList);
}
}
public string EditCustomer(Customer customer)
{
string msg;
try
{
if (ModelState.IsValid)
{
using (StoreEntities db = new StoreEntities())
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
msg = "Saved Successfully";
}
}
else
{
msg = "Did not save! Error from Edit Customer ";
}
}
catch (Exception ex)
{
msg = "Error occured:" + ex.Message;
}
return msg;
}
#endregion