0

ASP.NET MVCビューがありますが<input type='image'>、フォームの送信時に実行されるアクションで座標を取得する方法がわかりません。リクエストされたURLは次のようになります

/Map/View/?map.x=156&map.y=196

でも私にはできません

public ActionResult View( int map.x, int map.y )
{
  ...
}

これらは明らかにC#メソッドパラメーターの有効な名前ではないためです。ActionNameクエリパラメータをメソッドパラメータにマップするための属性に相当するものはありますか?

4

5 に答える 5

4

モデルバインダーを使用し、プレフィックスプロパティを「マップ」に設定する必要があります。

まず、Modelオブジェクトを作成します。

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

そしてあなたの行動方法では:

public ActionResult About([Bind(Prefix="map")]ImageMap map)
{

   // do whatever you want here
    var xCord = map.x;

}
于 2009-05-20T12:59:58.933 に答える
4

質問に直接答えるには、[バインド]を使用してパラメーターで使用されるフィールドを変更できます。

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);
    }
}
于 2009-05-20T13:24:21.833 に答える
1

次のようなクラスを作成できます。

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

次に、それをアクションメソッドのパラメーターとして使用します

public ActionResult View(ImageMap map)
{
  ...
}
于 2009-05-20T12:39:11.143 に答える
0

sを試してくださいIModelBinderこれこれこの質問を参照してください。

于 2009-05-20T12:39:15.497 に答える
0

次のリンクで提供されているコードを使用して、まったく異なるアプローチを試し、イメージマップを作成できます。

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

于 2011-06-27T09:48:49.443 に答える