1
Boa Tarde Personal,
I’m having trouble passing a csv that is being sent from ajax using jquery to the controller where null always arrives.
Could you help me?
View :
@using (Html.BeginForm("Importar", "Contatos", FormMethod.Post, new { enctype = "multipart/form-data",id="formUpload" }))
{
<div class="modal fade" id="importarCSV">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title"> <i class="icon-user"></i> Importar CSV</h4>
</div>
<div class="modal-body row">
<div class="col-lg-12">
@Html.TextBox("arquivoExcel", "", new { type = "file", required = "required" })
</div>
<div class="col-lg-12">
<br />
@Html.Label("O que deseja fazer com os contatos deste arquivo ?", null, new { @class = "lead" })
<br />
<div class="form-group">
@Html.RadioButton("0", "", new { id = "vinculaAtualizaG", required = "required" })
<small>Vincular e atualizar um grupo</small>
<br />
@Html.RadioButton("0", "", new { id = "vinculaNaoAtualizaC", required = "required" })
<small>Vincular a um grupo, sem atualizar os dados dos contatos</small>
<br />
@Html.RadioButton("0", "", new { id = "vinculaNovoGr", required = "required" })
<small>Vincular a um novo grupo, sem atualizar os dados dos contatos</small>
</div>
@Html.DropDownList("grupos", (List<SelectListItem>)ViewBag.Grupos, new { @class = "form-control" })
@Html.TextBox("grupooo", null, new { @class = "form-control text-box single-line" })
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancelar</button>
<button type="submit" class="btn btn-warning " id="dados">Importar</button>
</div>
</div>
</div>
</div>
}
Jquery:
$('#dados').on('click', function () {
var txtGrupo;
var option;
var radioSelected;
var radios = document.getElementsByName("0");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var id = $(radios[i]).attr("id");
radioSelected = id;
if (id == "vinculaNovoGr") {
txtGrupo = $("#grupooo").val();
} else {
option = $('#grupos').find(":selected").text();
}
}
}
var formData = new FormData($('#formUpload')[0]);
debugger;
$.ajax({
url: "/Contatos/Importar",
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
grupooo: $("#grupooo").val(),
id: radioSelected,
opcaoCombo: $('#grupos').find(":selected").text(), //,
arquivoExcel: formData
}),
async: false
});
});
CONTROLLER:
[HttpPost]
public ActionResult Importar(HttpPostedFileBase arquivoExcel,string teste, string id, string grupooo, string opcaoCombo)
{
var d = HttpContext.Request.Files[0];
var r = id;
var opc = grupooo;
var fs = opcaoCombo;
return RedirectToAction("Index", "Contatos");
}
You can’t do it
JSON.stringify()
of aFormData
. DoformData.append()
to add data and send theformData
without turning it into a string.– Marcelo Shiniti Uchimura
https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
– Marcelo Shiniti Uchimura
I believe that your post could not be
application/json
to upload, it should bemultipart/form-data
– Leandro Angelo