2011-06-19 6 views
2

を除外する称えるない方法Url.RouteUrl(object)を使用しているとき、私は、クエリ文字列に入ってから特定のプロパティを除外するのですか?MVC RouteUrlは</p> <p>(私はASP.NET MVC 2を使用しています)属性

具体的には、自分のビューに渡されているモデルオブジェクトで、私は除外したい文字列(技術的にはIEnumerable <の文字列>)の配列を持っています。

私はExcludeのBind属性がこれを行うと考えられていましたが、機能しません。私は私のクラスに[Bind(Exclude = "Sizes")]を入れてみましたが、私はこのように見えるURLの取得維持:

ます。http:匿名オブジェクトの// localhost /をサイズ=可能System.String []

答えて

2

拡張メソッドとレスキューへの救助!

/// <summary> 
/// Add UrlHelper extension methods that construct outgoing URL's but 
/// remove route values that are excluded by the Bind attribute's 
/// Include or Exclude. The methods to mirror are those that take an 
/// object as an argument: 
/// 
/// public string Action(string actionName, object routeValues); 
/// public string Action(string actionName, string controllerName 
///     , object routeValues); 
/// public string Action(string actionName, string controllerName 
///     , object routeValues, string protocol); 
/// 
/// public string RouteUrl(object routeValues); 
/// public string RouteUrl(string routeName, object routeValues); 
/// public string RouteUrl(string routeName, object routeValues 
///      , string protocol); 
/// </summary> 
public static class UrlHelperExtensions 
{ 
    public static string Action(this UrlHelper helper, string actionName 
           , object routeValues, bool onlyBoundValues) 
    { 
     RouteValueDictionary finalRouteValues = 
      new RouteValueDictionary(routeValues); 

     if (onlyBoundValues) 
     { 
      RemoveUnboundValues(finalRouteValues, routeValues); 
     } 

     // Internally, MVC calls an overload of GenerateUrl with 
     // hard-coded defaults. Since we shouldn't know what these 
     // defaults are, we call the non-extension equivalents. 
     return helper.Action(actionName, routeValues); 
    } 

    public static string Action(this UrlHelper helper, string actionName 
           , string controllerName, object routeValues 
           , bool onlyBoundValues) 
    { 
     RouteValueDictionary finalRouteValues = 
      new RouteValueDictionary(routeValues); 

     if (onlyBoundValues) 
     { 
      RemoveUnboundValues(finalRouteValues, routeValues); 
     } 

     return helper.Action(actionName, controllerName, finalRouteValues); 
    } 

    public static string Action(this UrlHelper helper, string actionName 
           , string controllerName, object routeValues 
           , string protocol, bool onlyBoundValues) 
    { 
     RouteValueDictionary finalRouteValues = 
      new RouteValueDictionary(routeValues); 

     if (onlyBoundValues) 
     { 
      RemoveUnboundValues(finalRouteValues, routeValues); 
     } 

     return helper.Action(actionName, controllerName 
           , finalRouteValues, protocol); 
    } 

    public static string RouteUrl(this UrlHelper helper, object routeValues 
            , bool onlyBoundValues) 
    { 
     RouteValueDictionary finalRouteValues = 
      new RouteValueDictionary(routeValues); 
     if (onlyBoundValues) 
     { 
      RemoveUnboundValues(finalRouteValues, routeValues); 
     } 
     return helper.RouteUrl(finalRouteValues); 
    } 

    public static string RouteUrl(this UrlHelper helper, string routeName 
            , object routeValues, bool onlyBoundValues) 
    { 
     RouteValueDictionary finalRouteValues = 
      new RouteValueDictionary(routeValues); 
     if (onlyBoundValues) 
     { 
      RemoveUnboundValues(finalRouteValues, routeValues); 
     } 
     return helper.RouteUrl(routeName, finalRouteValues); 
    } 

    public static string RouteUrl(this UrlHelper helper, string routeName 
            , object routeValues, string protocol 
            , bool onlyBoundValues) 
    { 
     RouteValueDictionary finalRouteValues = 
      new RouteValueDictionary(routeValues); 

     if (onlyBoundValues) 
     { 
      RemoveUnboundValues(finalRouteValues, routeValues); 
     } 

     return helper.RouteUrl(routeName, finalRouteValues, protocol); 
    } 

    /// <summary> 
    /// Reflect into the routeValueObject and remove any keys from 
    /// routeValues that are not bound by the Bind attribute 
    /// </summary> 
    private static void RemoveUnboundValues(RouteValueDictionary routeValues 
              , object source) 
    { 
     if (source == null) 
     { 
      return; 
     } 

     var type = source.GetType(); 

     BindAttribute b = null; 

     foreach (var attribute in type.GetCustomAttributes(true)) 
     { 
      if (attribute is BindAttribute) 
      { 
       b = (BindAttribute)attribute; 
       break; 
      } 
     } 

     if (b == null) 
     { 
      return; 
     } 

     foreach (var property in type.GetProperties()) 
     { 
      var propertyName = property.Name; 
      if (!b.IsPropertyAllowed(propertyName)) 
      { 
       routeValues.Remove(propertyName); 
      } 
     } 
    } 
} 
1

すべてのプロパティが使用されますがAFAIKにはいくつかを除外する方法がありません。 [Bind]属性はコントローラのアクション引数に使用され、モデルバインディングから除外またはモデルバインディングに含めるべきであるが、URLを構成するためにバインダープロパティにバインダープロパティを指定しません。あなたが一つずつたいプロパティを指定する必要があります

Url.RouteUrl(new { 
    Prop1 = Model.Prop1, 
    Prop2 = Model.Prop2, 
    action = "SomeAction", 
    controller = "SomeController" 
}) 

またはIDのみ含まれます:

Url.RouteUrl(new { 
    id = Model.Id, 
    action = "SomeAction", 
    controller = "SomeController" 
}) 

からモデルを取得するためには、このIDを使用して、ターゲットコントローラのアクションを持っているがいくつかの永続的なデータストア。

関連する問題