MVC Post Up File File and model to controller

时间:2016-01-30 00:33:24

标签: javascript c# jquery asp.net-mvc

我需要发布一个uploades文件和模型,我使用mvc这个视图中的代码:

@implementation TextFieldWithHighlighting
@synthesize backgroundStyle = _backgroundStyle;
- (NSBackgroundStyle) backgroundStyle {
    return _backgroundStyle;
}
@synthesize secretBackgroundStyle = _secretBackgroundStyle;
- (NSBackgroundStyle) secretBackgroundStyle {
    return _backgroundStyle;
}

- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle {

}

- (void) setSecretBackgroundStyle:(NSBackgroundStyle)secretBackgroundStyle {
    _backgroundStyle = secretBackgroundStyle;
    self.needsDisplay = YES;
}


- (void)drawRect:(NSRect)dirtyRect {
    if(_backgroundStyle == NSBackgroundStyleDark) {
        if(self.originalTextColor == nil) {
            self.originalTextColor = self.textColor;
        }
        self.textColor = [NSColor whiteColor];
    } else {
        if(self.originalTextColor) {
            self.textColor = self.originalTextColor;
        } else {
            self.textColor = [NSColor colorWithCalibratedRed:0x40/255.0 green:0x40/255.0 blue:0x41/255.0 alpha:0xFF/255.0];
        }
    }
    [super drawRect:dirtyRect];

}

@end

}

控制器代码:

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);
    }
});

它不起作用,请求不好。如果我把url:“/ Empleado / GuardarDocumento /”+ documento,我进入控制器代码,但模型为空。

有什么问题?我正在尝试向控制器发送上传的文件和模型,我该怎么做?

1 个答案:

答案 0 :(得分:0)

您发送到控制器操作的类型必须与操作所期望的类型相匹配。您正在发送FormData,并且您期待clsDocumentoEmpleadoModel