我尝试将FormData()
发送到ASP.NET MVC控制器,但相同的数据为空。
$('#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
});
});
public virtual async Task<ActionResult> CreatePrfile(CreateFreelancerProfileViewModel viewModel, bool isFirst, HttpPostedFileBase image)
public class CreateFreelancerProfileViewModel : BaseViewModel
{
#region Collocations
public ExternalProjectViewModel[] ExternalProjects { get; set; }
public CertificateUniverSityViewModel[] CertificateUniverSitys { get; set; }
public CertificateInstitutionsViewModel[] CertificateInstitutions { get; set; }
public SoicalViewModel Soical { get; set; }
#endregion
}
最佳答案
用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);
}