简体   繁体   English

我可以在Vue.js代码中访问C#元组值吗?

[英]Can I access a C# Tuple value in Vue.js code?

So here's the question, can you access returned C# Tuple variables in vue.js? 所以这是问题,您可以在vue.js中访问返回的C#元组变量吗?

Example, I have a vue.js function that on a refresh calls a C# action ('GetChartData') which returns a json result to it. 例如,我有一个vue.js函数,该函数在刷新时调用C#动作(“ GetChartData”),该动作将向其返回json结果。

    refreshChart: function () {
        var vm = this;
        $.getJSON(window.chartDataRoute, {
            fromDate: (somedate),
            toDate: (somedate)
        }).done(function (data) {
            vm.chartData = data;
        });
    },

So the json result of the C# function gets passed as a ContentResult type into vm.chartData in the vue.js code. 因此,C#函数的json结果作为ContentResult类型传递到vue.js代码中的vm.chartData中。 Here's the C# function: 这是C#函数:

    public ContentResult GetChartData(DateTime? fromDate, DateTime? toDate)
    {
        var pageStats = (go away and get some data);
        var chartModel = ChartHelpers.GetMainChartModel(chartPeriod, pageStats);
        var json = JsonConvert.SerializeObject(chartModel, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
        return new ContentResult { Content = json, ContentType = "application/json" };
    }

All pretty straighforward, but I need to return two new fields to the vue.js function, and I really don't want to get involved with rewriting the json result as the results are used in a graph function that is beyond the scope of what I'm trying to do. 一切都很简单,但是我需要将两个新字段返回给vue.js函数,而且我真的不想参与重写json结果,因为结果被用于超出函数范围的graph函数中我在努力

So, can I convert the function to a Tuple and pass two extra int values and then access them in the vue.js code? 因此,我可以将函数转换为元组并传递两个额外的int值,然后在vue.js代码中对其进行访问吗? Like this: 像这样:

    public Tuple<ContentResult,int,int> GetChartData(DateTime? fromDate, DateTime? toDate)
    {
        var pageStats = (go away and get some data);

        var totalClicks = (an int number);
        var totalViews = (an int number);

        var chartModel = ChartHelpers.GetMainChartModel(chartPeriod, pageStats);
        var json = JsonConvert.SerializeObject(chartModel, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

        // Can I return the extra data in a Tuple?
        return Tuple.Create(new ContentResult { Content = json, ContentType = "application/json" }, totalClicks, totalViews);
    }

However, this breaks the vue.js, as I suppose 'data' is now different and so the graphs don't work. 但是,这破坏了vue.js,因为我认为“数据”现在不同了,因此图形不起作用。 So I'd need to be able to assign the ContentResult to data (as it is now), and the two extra ints somehow. 因此,我需要能够将ContentResult分配给数据(现在是这样), 以某种方式将两个额外的int赋值。

If I can't do it like this then can I do it another way that doesn't involve rewriting any of the vue.js in depth? 如果我不能这样做,那么我可以做另一种不涉及深度重写任何vue.js的方法吗? One of problems is that I'm a bit of a vue.js novice... 问题之一是我有点vue.js新手...

Call the C# function twice one for chartdata and other for the other two properties. 两次调用C#函数,一次用于chartdata,另一次用于其他两个属性。 you also need to pass some Identifier for which the C# function get called either for chartdata or other two properties. 您还需要传递一些标识符,以获取ChartData或其他两个属性的C#函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 或 Vue.js 从文本文件中读取特定数据? - How can I read specific data from text file using C# or Vue.js? 如何在C#中使用元组作为键的字典中访问值? - How to access value in a Dictionary that has a Tuple as the Key in c#? 如何使用LINQ在元组中添加Where子句以在C#中返回单个值? - How can I use LINQ to add a Where clause to a Tuple to return a single value in C#? C# 7:如何使用元组将对象解构为单个值? - C# 7:How can I deconstruct an object into a single value using a tuple? 使用vue.js获取dropdownlist的价值 - getting value of dropdownlist using vue.js 如何在代码中访问C#性能计数器? - How can I access the C# performance counter in the code? 如何从 C# 代码访问 wpf 中的 ResourceDictionary? - How can I access ResourceDictionary in wpf from C# code? 使用 Vue.js 和 Axios 使用 REST API (C#) 返回的字符串 - Consume string returned by REST API (C#) with Vue.js and Axios 如何将现有的 Vue.js 2 接口集成到现有的 ASP.NET/C# Web 表单应用程序? - How to integrate an existing Vue.js 2 interface to an existing ASP.NET/C# Web forms application? 当映射到模型(axios/vue.js/c#)时,我的身体在我的后端到达 null - My body arrive null in my backend when mapping to the model(axios/vue.js/c#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM