使用ajax将FormData发送到ASP.NET MVC控制器

时间:2017-02-27 09:04:43

标签: javascript jquery ajax asp.net-mvc

我尝试将<img src='http://g.gravizo.com/g? /** *Structural Things *@opt commentname *@note Notes can *be extended to *span multiple lines */ class Structural{} /** *@opt all *@note Class */ class Counter extends Structural { static public int counter; public int getCounter%28%29; } /** *@opt shape activeclass *@opt all *@note Active Class */ class RunningCounter extends Counter{} '> 发送到ASP.NET MVC Controller,但相同的数据为空。FormData()

ExternalProjects, CertificateUniverSitys,...
$('#Controller').on('click', '#SaveProfile', function() {
  debugger;

  var CertificateInstitutions = JSON.parse(localStorage.getItem("CertificateInstitutionsListLocal"));
  var CertificateUniverSitys = JSON.parse(localStorage.getItem("CertificateUniverSitysListLocal"));
  var ExternalProjects = JSON.parse(localStorage.getItem("ExProjectListLocal"));

  var soical = {
    GithubId: $('#GithubId').val(),
    StackowerflowId: $('#StackowerflowId').val(),
    TweiterId: $('#TweiterId').val()
  }

  var SkillList = $('select.tagit-hiddenSelect :selected').map(function() {
    return this.value;
  }).get();

  var form = $('#CreateProfileForm');
  var token = $('input[name="__RequestVerificationToken"]', form).val();
  var file_data = $("#Image").prop("files")[0];
  var fd = new FormData();

  fd.append("isFirst", true);
  fd.append("token", token);
  fd.append("image", file_data);
  fd.append("soical", soical);
  fd.append("SkillList", SkillList);
  fd.append("ExternalProjects", ExternalProjects);
  fd.append("CertificateUniverSitys", CertificateUniverSitys);
  fd.append("CertificateInstitutions", CertificateInstitutions);

  $.ajax({
    url: '@postUrl',
    method: "POST",
    contentType: false,
    processData: false,
    data: fd
  });
});

1 个答案:

答案 0 :(得分:1)

使用ExternalProjects循环添加for

 var fd = new FormData();

 for (var i = 0; i < ExternalProjects.length ; i++) 
     {
        fd.append("ExternalProjects["+i+"].Name",ExternalProjects[i].Name);
        fd.append("ExternalProjects["+i+"].Body",ExternalProjects[i].Body);
        fd.append("ExternalProjects["+i+"].Url",ExternalProjects[i].Url);
     }