Jqgrid添加按钮使用控制器中的编辑代码 [英] Jqgrid add button is using code from edit in the controller

查看:94
本文介绍了Jqgrid添加按钮使用控制器中的编辑代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在摸着这个。我的代码看起来是正确的,但是当我在视图中选择添加一行时,实际上是在控制器中调用编辑。我知道这是因为我保存时出错了。我更改了控制器中的错误消息,以验证消息是否来自编辑代码。有没有我在这里看到的东西?谢谢你的帮助。



我的尝试:



Jqgrid

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

{
    // 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

[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

推荐答案

经过大量调试后,我发现Jqgrid必须处理索引。你输入JavaScript代码的顺序确实很重要。我在添加之前进行了编辑。切换它,以便在代码编辑工作之前添加。因此Jqgrid在代码中进入该区域,我猜测是通过索引编写的。由于编辑是添加应该是的位置,因此使用编辑添加按钮。



我可以说我不同意这种方法。但是我不能说。您会认为它只会在需要时随时随地获取代码。

经验教训。
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.


这篇关于Jqgrid添加按钮使用控制器中的编辑代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆