-1

I need post a uploades file and model, i use mvc this the code in the view:

function GuardarDocumento() {
var fileUpload;
var tipoDoc;
if (currentTabTitle == "#tab-AT") {
    fileUpload = $("#inputFileAT").get(0);
    tipoDoc = 'A';
}

var files = fileUpload.files;

var data = new FormData();
for (var i = 0; i < files.length; i++) {
    data.append(files[i].name, files[i]);
}

var documento = {
    Id_emd: 0,
    Id_emm: {
        Id_emm: id,
        Tipo: tipo,
    },
    Tipo_emd: tipoDoc,
    Fecha_emd: $("#txtFechaDocAT").val(),
    Nombre_emd: $("#txtNombreDocAT").val(),
}

$.ajax({
    url: "/Empleado/GuardarDocumento/" + JSON.stringify(documento),
    type: "POST",
    data: data,
    contentType: false,
    processData: false,
    success: function (data) {
        if (data.success) {
            alert("Documento Guardado con Éxito.");
            Cancelar();
        }
        else {
            alert(data.message);
        }
    },
    error: function (xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
    }
});

}

And the controller code:

[HttpPost]
    public ActionResult GuardarDocumento(clsDocumentoEmpleadoModel documento)
    {
        try
        {
            if (Request.Files.Count > 0)
            {
                clsEmpleadoBLL bll = new clsEmpleadoBLL();
                bll.PostedFile = Request.Files[0];
                documento.Archivo_emd = Request.Files[0].FileName;
                bll.AddDocument(documento);
            }
            else
            {
                return Json(new { success = false, message = "No ha seleccionado ningún archivo." });
            }
        }
        catch (Exception ex)
        {
            GENException.Write(ex, "EmpleadoController.GuardarDocumento");
            return Json(new { success = false, message = string.Format("Error: {0}", ex.Message) });
        }
        return Json(new { success = true, message = "Documento Guardado Correctamente." });
    }

It does not work, bad request. If I put url: "/Empleado/GuardarDocumento/" + documento, i get into controller code but model is null.

What is wrong? I am trying send to controller both uploaded file and model, how can I do that?

tereško
  • 58,060
  • 25
  • 98
  • 150
DanielVorph
  • 131
  • 7

1 Answers1

0

The type that you're sending to the controller action must match the type the action is expecting. You're sending a FormData, and you're expecting a clsDocumentoEmpleadoModel.

mariocatch
  • 8,305
  • 8
  • 50
  • 71
  • ok, but how can i send model and uploaded file to controller in one post ajax call? – DanielVorph Jan 30 '16 at 22:29
  • The type on the action needs to match what you're sending it. If you want your action to support a model and an upload file, then you need to change your model, or accept two parameters in your action. One for the model, and one for the uploaded file. And use the `data` property of the ajax call to pass them. – mariocatch Jan 30 '16 at 22:34
  • I add a property to my model with HttpPostedFileBase type, in the ajax call I assign this property File: files[0], but in the controller this property is always null the others properties have correct value, why is null? – DanielVorph Feb 02 '16 at 18:04