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