无法将属性从JsonDotNetValueProviderFactory绑定到我的控制器

时间:2018-12-05 02:52:42

标签: json asp.net-mvc c#-4.0 json.net

我正在使用JsonDotNetValueProviderFactory,并且在将数据绑定到控制器参数时遇到问题-我在List对象中没有任何数据,请您提供宝贵的输入信息。

基本上我正在使用angular js,这是一个excel文件上传,因为我无法使用这种方法上传大型excel数据

我尝试了以下链接 https://www.dalsoft.co.uk/blog/index.php/2012/01/10/asp-net-mvc-3-improved-jsonvalueproviderfactory-using-json-net/

JavaScript代码

testCachedData?.response.url // Ok
testCachedData?.isEmpty // false
testCachedData?.data // contains bytes

在global.asax中

$scope.SaveData = function (excelData) {



 var person = getPerson();

    //var json = JSON.stringify(person);

    var data = $.toJSON(excelData);

    $http({
        method: "POST",
        url: "/Home/ImportData",
        dataType: 'json',
        //data: JSON.stringify(excelData),
        data: { objects: data },
        //data: json
        headers: {
            'Content-Type': 'application/json'
        }
    }).then(function (data) {
        if (data.status) {
            $scope.Message = excelData.length + " record inserted";
        }
        else {
            $scope.Message = "Failed";
        }
    }, function (error) {
        $scope.Message = "Error";
    })
}

JsonDotNetValueProviderFactory类

ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

这是我的动作方法

我没有在这里获取任何数据到对象

 public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {

        IValueProvider vProvider = null;
        if (controllerContext == null)
            throw new ArgumentNullException("controllerContext");

        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            return null;

        var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
        var bodyText = reader.ReadToEnd();

        //dynamic objects = JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter());

        //foreach (var obj in objects)
        //{
        //vProvider = String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(objects, CultureInfo.CurrentCulture);
        //}

        return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()), CultureInfo.CurrentCulture);

        //return vProvider;
    }
}

0 个答案:

没有答案