Click here to Skip to main content
15,885,939 members

Jqgrid add button is using code from edit in the controller

Member 13812021 asked:

Open original thread
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
Tags: C#, JQGrid

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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