用于服务器端imagemap的ASP.NET MVC路由

时间:2021-10-02 08:22:20

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

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

/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?

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

5 个解决方案

#1


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

您必须使用Model binder并将prefix属性设置为“map”:

First create the Model object:

首先创建Model对象:

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;

}

#2


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

要直接回答您的问题,您可以使用[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.

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

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:

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

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

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

虽然您仍然可以使用[Bind(Prefix =“NotTheParameterName”)]重命名输入,但如果您愿意。

The code for ImageMapBinder is as follows:

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);
    }
}

#3


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)
{
  ...
}

#4


Try IModelBinders. See this, this and this question.

试试IModelBinders。看到这个,这个和这个问题。

#5


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

#1


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

您必须使用Model binder并将prefix属性设置为“map”:

First create the Model object:

首先创建Model对象:

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;

}

#2


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

要直接回答您的问题,您可以使用[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.

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

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:

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

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

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

虽然您仍然可以使用[Bind(Prefix =“NotTheParameterName”)]重命名输入,但如果您愿意。

The code for ImageMapBinder is as follows:

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);
    }
}

#3


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)
{
  ...
}

#4


Try IModelBinders. See this, this and this question.

试试IModelBinders。看到这个,这个和这个问题。

#5


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