asp.net mvc with angular with multiple parameter ajax call HttpPostedFileBase file getting null value

asp.net mvc with angular with multiple parameter ajax call HttpPostedFileBase file getting null value

这里我从我的 angular 服务发送一个对象和一个文件:

 $scope.addProject = function () {
        {
            var project = {};
            project["Id"] = $scope.Id;
            project["ProjectCode"] = $scope.ProjectCode;

            var file = $scope.myFile;
            projectModelService.saveProject(project,file)
            .success(function (data) {

            })
            .error(function (xhr) {
                $ngBootbox.alert('Error in saving data');
            });

        }
    };

在我的 angular 服务中:

saveProject: function (project) {
             var parameters = { project: project, file: file };

             var fdata = new FormData();
             fdata.append('file', file);

             var param = { project: project, file: fdata.append('file', file) };
            return $http({
                url: '/Project/CreateProject',
                method: 'POST',
                data: project
               // contentType: false, // Not to set any content header
               // processData: false, // Not to process data
               // transformRequest: angular.identity,
               // headers: { 'Content-Type': undefined },
                dataType: "json",
                data: JSON.stringify(param)

            })
        },

在我的 Project/Createproject 中:

  public JsonResult CreateProject(Project project, HttpPostedFileBase file)
    {
     }

现在,我的 HttpPostedFileBase 文件 return 为空。但是如果我通过单个参数传递文件:

 uploadProjectModelList: function (file) {
            var fdata = new FormData();
            var url = '/Project/UploadProjectFGModelListExcelFile';
            fdata.append('file', file);
            return $http.post(url, fdata, {
                transformRequest: angular.identity,
                headers: { 'Content-Type': undefined }
            })
        }

它显示文件值,但每当使用它传递多个参数时,就像找到项目对象文件一样,但项目对象为空。我如何将多个参数作为对象和文件从 ajax 传递到 MVC5 中的 asp.net 控制器?

您可以将 project 参数作为 FormData 负载的一部分传递:

var fdata = new FormData();
var url = '/Project/UploadProjectFGModelListExcelFile';
fdata.append('file', file);

fdata.append('project.Id', project.id);
fdata.append('project.Foo', project.foo);
fdata.append('project.Bar', project.bar);
fdata.append('project.Baz.Bazinga', project.baz.bazinga);
...

return $http.post(url, fdata, {
    transformRequest: angular.identity,
    headers: { 'Content-Type': undefined }
});

标准字段命名规则适用于 ASP.NET MVC 模型绑定器。