ASP.NET MVC route for map servers

I have an <input type='image'>

ASP.NET MVC view, but I'm not sure how to get the coordinates in the action that is executed 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 )
{
  ...
}

      

as they are clearly not valid names for C # method parameters. Is there any attribute equivalent ActionName

for mapping request parameters to method parameters?

0


a source to share


5 answers


You must use the binding Model object and set the prefix property to "map":

First, create a 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;

}

      

+4


a source


To answer your question directly, you can change the field that is used for the parameter using [Bind]:

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

      

However, a custom ModelBinder that tied the image map to the System.Drawing.Point structure would be nicer.

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



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

      

Although you can still rename the input using [Bind(Prefix="NotTheParameterName")]

if you like.

The code for ImageMapBinder looks like this:

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

      

+4


a source


You can create a class like this:

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

      

and then use that as a parameter to your action method

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

      

+1


a source


Try IModelBinder

s. See this , this and this question .

0


a source


You can try a completely different approach and create an image map using the code given in the following link.

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

0


a source







All Articles