Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
JavaScript
{
    // add option
    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";
                }
            }

        }
    }
},
{
    // edit option
    zIndex: 100,
    url: '/Admin/EditCustomer',
    closeOnEscape: true,
    closeAfterEdit: true,
    recreateForm: true,
    afterComplete: function (response) {
        if (response.responseText) {
            alert(response.responseText);
        }
    }
},

Controller
C#
[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 (Exception ex)
    //{
    //    var errormessage = "Error occured: " + ex.Message;
    //    return Json(errormessage, 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
Posted
Updated 12-Aug-18 6:53am
Comments
Mike V Baker 9-Aug-18 12:36pm    
"...when I select in the view to add a row it is actually calling the edit in the controller.."
Is it possible that it thinks you have edited the row that's losing focus? You click view to add a new record. In order to do that it has to leave the record that was selected on startup...? What's the value of customer at that point? Perhaps it's null and all you need is a check for a null entity to exit.
Member 13812021 10-Aug-18 8:48am    
Right now the table is empty. There is no row selected. I will put a record in the table manually as I did before to make sure it was showing in the grid. I deleted them with the delete button so both of those work. I also have a users table that appears to be working but it already has records in the table. I will post my findings. However if this is the case, why does it require that there be data in the table before it will work?

Okay added a record manually. Not sure what is going on. It still uses the edit and when I go to edit a record it saves it in the table as a new record. So edit is using add and add is using edit????
Mike V Baker 10-Aug-18 9:08am    
The code that would invoke the save isn't posted so I'm not sure, but what I thought might be happening is that the save is triggered by some event in the grid. It appears that the event is being triggered for you when there's no currently selected record. So the controller gets called with a customer == null. Are you able to break the operation and view the call stack to see from where the update is being called?
Member 13812021 10-Aug-18 9:30am    
The save is the 'Create Customer' in the controller section above. I will have to look at it later. The same code is being used in another table and it works, that is what it confusing to me. I thought I may have missed something in the copy.

1 solution

After much debugging I have found that Jqgrid must work on Indexing. It does matter what order you put the JavaScript code in. I had the edit before the add. switching it so that the add was before the edit in the code works. So Jqgrid goes to the area, I am guessing by indexing, in the code. Since edit was where the add should have been it was using the edit for the add button.

I can say that I do not agree with this method. However it is not mine to say. You would think that it would just grab the code where ever and whenever it is needed.
Lesson learned.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900