如何在MVC4中捕捉x和y位置并重新定位?

时间:2013-01-10 08:15:56

标签: javascript asp.net-mvc

  

可能重复:
  Dynamically retrieve the position (X,Y) of an HTML element

在一个MVC4应用程序(针对IE7 +)中,我有一个图像。我希望用户能够点击图像来定位一个小标记(图像),我想存储与图像相关的相对x / y位置,这样当用户返回到此页面时我可以重新定位此图钉试。

实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以使用<input type="image" />标记 - 这类似于<input type="submit" />,但它会将点击的x和y坐标发布回服务器。

视图

<h1>Click on the image to place a pin</h1>
@using(Html.BeginForm())
{
    <input type="image" name="coords" src="http://placehold.it/300x300" />
}

控制器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(FormCollection form)
    {
        int x = int.Parse(form["coords.x"]);
        int y = int.Parse(form["coords.y"]);

        // FIXME Save location of pin

        return View();
    }

}