在会话中存储鼠标坐标并添加到 xml 字符串 (C# .NET MVC)

本文关键字:字符串 xml NET MVC 添加 会话 存储 鼠标 坐标 | 更新日期: 2023-09-27 18:35:56

基本上我的目标是使用JQuery将x&y鼠标坐标回发到我的控制器,其中2个值被添加到数组/列表中,然后添加到会话中。

我想循环访问一系列 60 次单击(随后回发到控制器),然后再构建包含每个坐标的 XML 字符串。

大致如下:

<xml>
<click1>
 <xpos>45</xpos>
 <ypos>55</ypos>
</click1>
<click2>
 <xpos>45</xpos>
 <ypos>55</ypos>
</click2>
</xml>

我不确定如何在每次回发后最好地存储单个鼠标单击,我认为也许会话和多维数组/列表对此有好处? 对于如何将会话转换回列表并根据需要组装XML,我也有些困惑。

谁能指出我正确的方向?

非常感谢!

赛克思。

在会话中存储鼠标坐标并添加到 xml 字符串 (C# .NET MVC)

我通过创建一个类型的字典来解决这个问题,鼠标坐标指的是一个接受 2 个字符串值的类。

然后,我将字典放入会话中,在需要向其添加新值时将其重新转换为字典。

最后,我在点击周期结束时遍历了字典,检索了所有值。

我希望这段代码对某人有所帮助:

var coords = new Dictionary<int, MouseCoordinates>();
HttpContext.Session.Add("coords", coords);
// Accessing Dictionary From Session
var coords = (Dictionary<int, MouseCoordinates>)HttpContext.Session["coords"];
// Adding Values using a integer stage that's posted back to the controller
coords.Add(currentStage, new MouseCoordinates { xposition = xpos, yposition = ypos });
// Looping through the session at the end of the cycle
            foreach (var ords in coords)
            {
                var qnode = ords.Key;
                var xvalue = ords.Value.xposition;
                var yvalue = ords.Value.yposition;
            }