Search code examples
asp.net-mvcasp.net-mvc-routingimagemap

ASP.NET MVC route for serverside imagemap


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?


Solution

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