每当调用PartialView时,确保视图控制器方法获取最新的图表数据(Ensuring view controller method to get latest chart data is called whenever PartialView called)
我在Web.Helpers中使用Chart来呈现视图的图形。 这个视图的数据集可以通过类型区分为子集,并且该视图允许用户选择类型。
包含此图表的我的剃须刀视图(_Chart.schtml)很简单:
<p> <img src="/MyController/MyChart" /> </p>
控制器代码(简化)
public void MyChart() { ChartSeries[] data = GetChartData(new string[] { _curChartDataType }); var chart = new Chart(500, 200, ChartTheme.Green); for (int i=0; i<data.Length; i++) { title += data[i].name + " "; chart.AddSeries(name: data[i].name, chartType: "Line", xValue: data[i].xValue, xField: data[i].xField, yValues: data[i].yValues, yFields: data[i].yFields); } chart.AddTitle(title) .AddLegend() .Write("png"); } public ActionResult ObjectList() { // irrelevant code removed return PartialView("_Chart"); }
在父视图的cshtml中,我使用jquery来捕获用户对类型选择的更改,并调用ajax中的MyController / ObjectList调用。
第一次加载视图时,图形正确呈现。 但是,用户调用的类型更改会导致后续对ObjectList的调用(如预期的那样),但MyChart()在第一次后再也不会被调用。
任何我应该做的事情来确保MyChart()每次调用PartialView(“_ Chart”)时都会被调用? 任何建议感激。
I am using Chart in Web.Helpers to render graphs for a view. The data set for this view can be distinguished by type into subsets, and the view allows the user to select a type.
My razor view containing this graph (_Chart.schtml) is simply:
<p> <img src="/MyController/MyChart" /> </p>
The controller code (simplified)
public void MyChart() { ChartSeries[] data = GetChartData(new string[] { _curChartDataType }); var chart = new Chart(500, 200, ChartTheme.Green); for (int i=0; i<data.Length; i++) { title += data[i].name + " "; chart.AddSeries(name: data[i].name, chartType: "Line", xValue: data[i].xValue, xField: data[i].xField, yValues: data[i].yValues, yFields: data[i].yFields); } chart.AddTitle(title) .AddLegend() .Write("png"); } public ActionResult ObjectList() { // irrelevant code removed return PartialView("_Chart"); }
In the parent view's cshtml, I use jquery to catch the user's change in type selection and invoke the call to MyController/ObjectList in ajax.
The graph renders properly when the view is first loaded. However, the user-invoked type changes result in a subsequent call to ObjectList (as expected), but MyChart() never gets called again after the first time.
Anything I should do to ensure MyChart() gets called again whenever PartialView("_Chart") is called? Any suggestions appreciated.
原文:https://stackoverflow.com/questions/36848191
最满意答案
这是一个特别模糊的功能吗?
是的,转换运营商不常使用。 我见过的地方是用户定义的类型,可以降级为内置类型。 像支持从原子数类型转换到/从原子数类型的固定精度数字类。
这是相对便携吗?
据我所知,它是。 他们永远处于标准之中。
用户定义的转换可以完成用户定义的类型吗?
是的,这是构造函数的特点之一。 一个构造函数能够有效地创建一个从参数类型到类的类型的转换运算符。 例如,像这样的一个类:
class Foo { public: Foo(int n) { // do stuff... } }
让你做:
Foo f = 123;
如果你之前使用过
std::string
,那么你已经使用了这个特性而没有意识到它。 (顺便说一句,如果要防止这种行为,请使用explicit
声明任何单参数构造explicit
。)Is this a particularly obscure feature?
Yes, conversion operators aren't used very often. The places I've seen them are for user-defined types that can degrade to built-in ones. Things like a fixed-precision number class that supports converting to/from atomic number types.
Is this relatively portable?
As far as I know, it is. They've been in the standard forever.
Can user-defined conversions to user defined types be done?
Yes, that's one of the features of constructors. A constructor that takes a single argument effectively creates a conversion operator from the argument type to your class's type. For example, a class like this:
class Foo { public: Foo(int n) { // do stuff... } }
Let's you do:
Foo f = 123;
If you've used
std::string
before, odds are you've used this feature without realizing it. (As an aside, if you want to prevent this behavior, declare any single-argument constructors usingexplicit
.)
相关问答
更多-
当您的类的实例位于添加的右侧时,您需要实现__radd__方法来处理这种情况。 文档说: 调用这些方法来实现二进制算术运算(+, - ,*,@,/,//,%,divmod(),pow(),**,<<,>>,&,^,|)反映(交换)的操作数。 这些函数仅在左操作数不支持相应操作且操作数具有不同类型时才会调用。 2例如,要评估表达式x-y,其中y是具有rsub ()方法的类的实例,y。 如果是x,则调用rsub (x)。 sub (y)返回NotImplemented。 例: class tInt(int): ...
-
用户定义的转换在C ++中(User Defined Conversions in C++)[2024-03-10]
这是一个特别模糊的功能吗? 是的,转换运营商不常使用。 我见过的地方是用户定义的类型,可以降级为内置类型。 像支持从原子数类型转换到/从原子数类型的固定精度数字类。 这是相对便携吗? 据我所知,它是。 他们永远处于标准之中。 用户定义的转换可以完成用户定义的类型吗? 是的,这是构造函数的特点之一。 一个构造函数能够有效地创建一个从参数类型到类的类型的转换运算符。 例如,像这样的一个类: class Foo { public: Foo(int n) { // do stuff... ... -
C ++用户定义的向量(C++ User-Defined Vector)[2022-08-07]
您希望让用户为您命名,并能够将其与向量相关联吗? 这就是std::map的用途, std::string作为键类型, std::vector作为有效负载类型。 You want to have users give you a name and to be able to associate that with a vector of things? That's what a std::map is for, with a std::string as the key type and a std::ve ... -
用户定义和标准转换(User-defined and standard conversions)[2023-01-03]
没有涉及引用的标准转换,因此此规则不相关。 相反,我们需要初始化引用的规则,在C ++ 11 8.5.3中给出。 这些都很复杂; 这里的相关内容是第5条的最后一个子弹(对于初始化程序与引用类型不是引用兼容的情况): 使用非参考拷贝初始化的规则,从初始化表达式创建并初始化类型为cv1 T1的临时类型 这里, cv1 T1是const CL1 。 无法从int创建临时T1 ,因此初始化失败。 编译器不需要搜索从引用类型派生或可转换为引用类型的所有类型; 它只考虑引用类型本身。 您必须指定要创建CL2 : fun ... -
您在两个类中都定义了Alpha(Beta x) ,因此应该使用哪个是模糊的。 允许每个类仅处理转换为自身。 换句话说, struct Alpha实现了Alpha(Beta b)因为它最好知道如何创建自己的实例。 另外,我会考虑实现显式转换而不是隐式转换。 它有时可能会意外地导致错误或意外转换,并且在处理复杂类时通常会进行“有损”转换。 public struct Alpha { internal string value; public static implicit operator A ...
-
在您的代码中,用户定义类型和基本类型之间的处理没有区别。 这两行的行为有什么不同: foo(a); foo(3.3); 是a是左值, 3.3是右值。 rvalue参数匹配您的重载1 (仅接受rvalues),而lvalue参数不匹配。 如果您尝试使用rvalue参数调用foo ,它也将匹配1并失败,例如foo(A{}); 。 In your code, there is no difference in treatment between user-defined types and primiti ...
-
你需要的是显式构造函数: struct I { explicit I( V& v ){} }; struct CI { explicit CI( const V& v ){} //I would like to say const only }; 太多的C ++程序员忽略了构造函数的显式关键字。 默认情况下,所有一元参数化构造函数都应该是显式的。 隐式构造函数会引发像这样的歧义问题,并导致非常愚蠢,迂回的转换过程,这些过程很容易导致代码有问题且效率低下。 现在你已经设置好了,歧义问题 ...
-
让我们首先看看当我们遵循各种规则时会发生什么。 遵循C#4.0规范中的规则: 搜索用户定义转换的类型集D由A和B组成。 适用转换的集合U包括用户定义的从A到B的隐式转换,以及从A提升的用户定义隐式转换? 到B? 我们现在必须选择U这两个元素中最独特的一个。 最具体的来源类型是A?。 最具体的目标类型是B. 你不包含A的任何转换? 到B,所以这是模棱两可的。 这应该是有道理的。 我们在这里不知道转换是否应该使用提升转换,从A转换? 到B? 然后从B? 到B,或者我们是否应该使用未提升的转换,从A转换? 到A然 ...
-
这是标准Stack类的扩展方法(你可以稍微重写一下,并在你自己的Stack类中使用类似的实例方法): public static class MyStackExtensions { public static Stack
Convert ( this Stack stack, Func converter = null) { if (stack == null) ... -
在c ++ 11中使用用户定义的转换重载运算符推导(overloaded operator deduction using user-defined conversions in c++11)[2023-01-08]
成员比较运算符不允许在左侧进行隐式转换,您需要使运算符成为非成员函数: class Complex { public: int data; Complex(int i) : data(i) {} }; bool operator < (const Complex& lhs, const Complex& rhs) { return lhs.data < rhs.data; } 现场演示 Member comparison operators do not allow for implic ...