jqGrid Lose data on submit

前提是你 提交于 2021-02-11 13:26:49

问题


I'm working on a CRUD application in ASP.net C #.

My problem is that when I click the Submit button and ModelState.IsValid = false, the data in the grid is lost. Does anyone know how to fix this?

I have a form in html and razor, I could not leave all the work to an ajax request, so what I did was add the onsubmit event to the form with the function "sendingContacts", This function gets the grid information in a JSON string and then I assign it in a hidden field in the form, so my controller can get all the information.

Html

        @using (Html.BeginForm("Nuevo", "Clientes", FormMethod.Post, new { id = "Form", onsubmit = "sendingContactos();", enctype = "multipart/form-data" }))
{

    @Html.Hidden("lista_contactos");

    .... A lot of razor inputs and selects
        <div class="form-group row">
                                                    @Html.LabelFor(p => p.nombre_comercial)
                                                    @Html.TextBoxFor(p => p.nombre_comercial, new { @class = "form-control" })
                                                    @Html.ValidationMessage("nombre_comercial", new { @class = "text-danger" })
                                                </div>

    ... And then jqgrid
    <div class="jqGrid_wrapper p-xs" style="overflow: hidden;">
                                <table id="grid"></table>
                                <div id="cc-footer"></div>
                                <div id="cc-toolbar"></div>
                            </div> }

Javascript

            var lastsel;
        jQuery("#grid").jqGrid({
            caption: " ",
            datatype: "local",
            autowidth: true,
            viewrecords: true,
            rownumbers: true,
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: '#cc-footer',
            colNames: [
                'Nombre',
                'Apellido Paterno',
                'Apellido Materno',
                'Departamento',
            ],
            colModel: [
                { name: 'nombre', editable: true, },
                { name: 'appaterno', editable: true, },
                { name: 'apmaterno', editable: true, },
                {
                    name: 'departamento',
                    index: 'departamento',
                    editable: true,
                    edittype: 'select',
                    editoptions: {
                        size: 1,
                        dataUrl: cl,
                        buildSelect: function (data) {

                            var response = jQuery.parseJSON(data);
                            var s = '<select>';
                            s += '<option></option>';
                            jQuery.each(response, function (i, item) {
                                s += '<option value="' + response[i].cve_departamentos + '">' + response[i].nombre + '</option>';
                            });
                            return s + "</select>";
                            console.log(data);
                        }

                    },
                    editrules: { required: true, edithidden: true },
                    recreateForm: true
                },

            ],
            editurl: "clientArray",

        });

OnSubmit

function sendingContactos() {

var grid = $('#grid').jqGrid('getRowData');   

var clientes_contactos = {
    "clientes_contactos": grid
};

$("#lista_contactos").val(JSON.stringify(clientes_contactos));

}

Controller

        [HttpPost]
    public async Task<ActionResult> Nuevo(Clientes cliente, HttpPostedFileBase file)
    {
        List<ClientesContactos> cc = new List<ClientesContactos>();
        var myJObject = JObject.Parse(cliente.lista_contactos);
        cc = JsonConvert.DeserializeObject<List<ClientesContactos>>(myJObject["clientes_contactos"].ToString());
        cliente.clientes_contactos = cc;


        if (!ModelState.IsValid)
        {
            GettingCombos();
            return View(cliente);
        }
        else
        {
            byte[] foto = null;
            if (file != null)
            {
                BinaryReader lector = new BinaryReader(file.InputStream);
                foto = lector.ReadBytes((int)file.ContentLength);
                cliente.cve_logo = foto;
            }

            try
            {
                await ClientHelper.Post("api/Clientes/Add", cliente);
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, "Error al insertar registro." + e);
            }

            return RedirectToAction("Index");
        }
    }

来源:https://stackoverflow.com/questions/60081600/jqgrid-lose-data-on-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!