controller does not receive data from ajax

64
November 16, 2019, at 6:10 PM

well in my learning of .net core with mvc template I have a challenge and it is to send an image and data of some herlpers to a cotrolator with ajax he tried several ways already but I always find the error that neither my object model is null and in the HttpContext.Request.Form comes the error: Type of error not detected: Error when building 'FormData': parameter 1 is not of type 'HTMLFormElement'. I would like to know if there is any need to initialize any service in the startup or something in my code that I leave is wrong

     <form id="formRegisterProcuct" enctype="multipart/form-data" name="formRegistrarProducto">
                    <div class="modal-body">
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.archivo)
                            @Html.TextBoxFor(d => d.ModeloProducto.archivo, "", new { type = "file", @class = "form-control", @ID = "txtImagen", @placeholder = "*******" })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.archivo, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.nombre)
                            @Html.TextBoxFor(d => d.ModeloProducto.nombre, "", new { @class = "form-control", @ID = "txtNombre", @placeholder = "Banano" })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.nombre, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.descripcion)
                            @Html.TextBoxFor(d => d.ModeloProducto.descripcion, "", new { @class = "form-control", @ID = "txtDescripcion", @placeholder = "Racimo de bananos " })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.descripcion, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.catalogo)
                            @Html.DropDownListFor(d => d.ModeloProducto.catalogo, Model.listCatalogos, "", new { @class = "form-control", @ID = "dplCatalogo", @placeholder = "Verduraq" })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.catalogo, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.cantidad_final)
                            @Html.TextBoxFor(d => d.ModeloProducto.cantidad_final, "", new { @class = "form-control", @ID = "txtCantidaFinal", @placeholder = "10" })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.cantidad_final, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.tipoCantidad)
                            @Html.DropDownListFor(d => d.ModeloProducto.tipoCantidad, Model.listCantidad, "", new { @class = "form-control", @ID = "txtTipoCantidad", @placeholder = "Racimo" })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.tipoCantidad, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.valor_total)
                            @Html.TextBoxFor(d => d.ModeloProducto.valor_total, "", new { @class = "form-control", @ID = "txtValorTotal", @placeholder = "100000" })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.valor_total, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(d => d.ModeloProducto.valor_unidad)
                            @Html.TextBoxFor(d => d.ModeloProducto.valor_unidad, "", new { @class = "form-control", @ID = "txtValorUnidad", @placeholder = "10000" })
                            @Html.ValidationMessageFor(d => d.ModeloProducto.valor_unidad, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="botonModal">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" value="Crear" class="btn btn-warning text-white" />
                    </div>
                </form>
     $('#formRegisterProcuct').submit(function (e) {
                         e.preventDefault();
                        var URL = "@Url.Action("GuardarProducto", "Producto")";
                        var form = $(this);
                        //var datos = new FormData(document.formRegistrarProducto);
                        var datos = new FormData(document.getElementById("formRegisterProcuct"));
                        $.ajax({
                            url: URL,
                             method: form.attr("post"),
                            //data: form.serialize(),                        
                            data: datos,
                            contentType: false,
                            processData: false,
                            success: function (data) {
                            switch (data.result) {
                                case true:
                                    document.location.href = "@Url.Action("MisProductos","Producto")";
                                    break;
                                case false:
                                        $("div.message").remove();
                                        $("#alertBody").after("<div class='message'>" + data.mensaje + "</div>");
                                        $('#modalAlerta').modal('show');
                                    //Hacemos desaparecer el modal
                                    setTimeout(function () { $('#modalAlerta').modal('hide'); }, 9000);
                                    break;
                                default:
                                    $("#formRegisterProcuct").html(data);
                                    break;
                            }
                        }
                            , error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log();
                        }
                    });
                     });

     public IActionResult GuardarProducto(Producto model, [FromServices]IDaoService DAO)
            {
                    var archivo = HttpContext.Request.Form.Keys;
                //var archivo = HttpContext.Request.Form["FormData"];}
     public class Producto
        {
            [Display(Name = "Nombre")]
            public string nombre { get; set; }
            [Display(Name = "Descripcion")]
            public string descripcion { get; set; }
            [Display(Name = "Cantidad")]
            public int cantidad_final { get; set; }
            [Display(Name = "Valor Todo")]
            public decimal valor_total { get; set; }
            [Display(Name = "Valor unidad")]
            public decimal valor_unidad { get; set; }
            [Display(Name = "Imagen")]
            public IFormFile archivo { get; set; }
            [Display(Name = "Catalogo")]
            public int catalogo { get; set; }
            [Display(Name = "Tipo")]
            public int tipoCantidad { get; set; }

            public int id_usuario { get; set; }
            public string imagen { get; set; }
        }
READ ALSO
How to update the foreign key value of table 2 from the primary key of table 1

How to update the foreign key value of table 2 from the primary key of table 1

Currently, I have 2 tables which table 1 has the primary key of customer_id and table 2 has the foreign key of customer_idMy issue is, I tried to update the value of the foreign key from the value of primary key, but the value is always 0 for foreign...

64
loading data into combo object with sql query result in Java

loading data into combo object with sql query result in Java

I would like to load a combo object with a result of sql queryI do not know how to convert List into String[]

54