服务器端图像映射的 ASP.NET MVC 路由

发布于 2024-07-19 20:55:26 字数 339 浏览 13 评论 0原文

我在 ASP.NET MVC 视图中有一个 ,但我不确定如何检索表单运行时运行的操作中的坐标已提交。 请求的 URL 看起来像,

/Map/View/?map.x=156&map.y=196

但我不能这样做,

public ActionResult View( int map.x, int map.y )
{
  ...
}

因为它们显然不是 C# 方法参数的有效名称。 是否有与 ActionName 属性等效的属性来将查询参数映射到方法参数?

I've got an <input type='image'> in an ASP.NET MVC view, but I'm not sure how to retrieve the coordinates in the action that runs when the form is submitted. The requested URL looks like

/Map/View/?map.x=156&map.y=196

but I can't just do

public ActionResult View( int map.x, int map.y )
{
  ...
}

because they're obviously not valid names for C# method parameters. Is there any equivalent of the ActionName attribute to map query parameters to method parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞎闹 2024-07-26 20:55:26

要直接回答您的问题,您可以使用 [Bind] 更改参数上使用的字段:

public ActionResult View([Bind(Prefix="map.x")] int x, 
    [Bind(Prefix="map.y")] int y )

但是,将图像映射绑定到 System.Drawing.Point 结构的自定义 ModelBinder 会更好。

编辑: 这是一个自动映射到 System.Drawing.Point 参数的 ImageMapBinder。 只要将以下代码添加到 Application_Start 中,您就不必使用属性来装饰每个 Point 参数:

ModelBinders.Binders.Add(typeof(Point), new ImageMapBinder());

尽管您仍然可以使用 [Bind(Prefix="NotTheParameterName")] 重命名输入如果你想。

ImageMapBinder的代码如下:

public class ImageMapBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext)
    {
        int x, y;

        if (!(ParseValue(bindingContext, "x", out x) &&
            ParseValue(bindingContext, "y", out y)))
        {
            return Point.Empty;
        }

        return new Point(x, y);
    }

    private bool ParseValue(ModelBindingContext bindingContext, string value, 
        out int outValue)
    {
        string key = String.Concat(bindingContext.ModelName, ".", value);

        ValueProviderResult result = bindingContext.ValueProvider[key];

        if (result == null)
        {
            outValue = 0;
            return false;
        }

        return ParseResult(result, out outValue);
    }

    private bool ParseResult(ValueProviderResult result, out int outValue)
    {
        if (result.RawValue == null)
        {
            outValue = 0;
            return false;
        }

        string value = (result.RawValue is string[])
            ? ((string[])result.RawValue)[0]
            : result.AttemptedValue;

        return Int32.TryParse(value, out outValue);
    }
}

To answer your question directly, you can change the field which is used on a parameter by using [Bind]:

public ActionResult View([Bind(Prefix="map.x")] int x, 
    [Bind(Prefix="map.y")] int y )

However, a custom ModelBinder that bound an image map to a System.Drawing.Point struct would be nicer.

Edit: Here is an ImageMapBinder that automatically maps to a System.Drawing.Point argument. You don't have to decorate each Point argument with an attribute as long as you add the following code to your Application_Start:

ModelBinders.Binders.Add(typeof(Point), new ImageMapBinder());

Though you can still rename the input using [Bind(Prefix="NotTheParameterName")] if you want to.

The code for ImageMapBinder is as follows:

public class ImageMapBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext)
    {
        int x, y;

        if (!(ParseValue(bindingContext, "x", out x) &&
            ParseValue(bindingContext, "y", out y)))
        {
            return Point.Empty;
        }

        return new Point(x, y);
    }

    private bool ParseValue(ModelBindingContext bindingContext, string value, 
        out int outValue)
    {
        string key = String.Concat(bindingContext.ModelName, ".", value);

        ValueProviderResult result = bindingContext.ValueProvider[key];

        if (result == null)
        {
            outValue = 0;
            return false;
        }

        return ParseResult(result, out outValue);
    }

    private bool ParseResult(ValueProviderResult result, out int outValue)
    {
        if (result.RawValue == null)
        {
            outValue = 0;
            return false;
        }

        string value = (result.RawValue is string[])
            ? ((string[])result.RawValue)[0]
            : result.AttemptedValue;

        return Int32.TryParse(value, out outValue);
    }
}
玩物 2024-07-26 20:55:26

您必须使用模型绑定器并将前缀属性设置为“map”:

首先创建模型对象:

public class ImageMap()
{
  public int x{get;set;}
  public int y{get;set;}
}

并在您的操作方法中:

public ActionResult About([Bind(Prefix="map")]ImageMap map)
{

   // do whatever you want here
    var xCord = map.x;

}

You have to use use Model binder and set the prefix property to "map":

First create the Model object:

public class ImageMap()
{
  public int x{get;set;}
  public int y{get;set;}
}

And in your action method:

public ActionResult About([Bind(Prefix="map")]ImageMap map)
{

   // do whatever you want here
    var xCord = map.x;

}
沫雨熙 2024-07-26 20:55:26

您可以创建一个这样的类:

public class ImageMap()
{
  public int x{get;set;}
  public int y{get;set;}
}

然后将其用作操作方法的参数

public ActionResult View(ImageMap map)
{
  ...
}

You could make a class like this:

public class ImageMap()
{
  public int x{get;set;}
  public int y{get;set;}
}

and then use that as a parameter for your action method

public ActionResult View(ImageMap map)
{
  ...
}
乞讨 2024-07-26 20:55:26

尝试 IModelBinder。 请参阅这个问题

Try IModelBinders. See this, this and this question.

国产ˉ祖宗 2024-07-26 20:55:26

您可以尝试一种完全不同的方法,并使用以下链接中提供的代码构建图像映射。

http://www.avantprime.com /articles/view-article/9/asp.net-mvc-image-map-helper

You can try an entirely different approach and build up your image map using the code provided in the following link.

http://www.avantprime.com/articles/view-article/9/asp.net-mvc-image-map-helper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文