質問に直接答えるには、[バインド]を使用してパラメーターで使用されるフィールドを変更できます。
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);
}
}