如何创建与视图模型属性的ActionLink的 [英] How to create an ActionLink with Properties for the View Model

查看:176
本文介绍了如何创建与视图模型属性的ActionLink的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有,我用它来筛选我的数据很多属性过滤器属性视图模型

I have a ViewModel with a Filter property that has many properties that I use to filter my data

例如:

class MyViewModel : IHasFilter
{
     public MyData[] Data { get; set; }
     public FilterViewModel Filter { get; set; }
}

class FilterViewModel
{
    public String MessageFilter { get; set; }
    //etc.
}

这用我查看时,工作正常。我可以设置 Model.Filter 的属性,它们传递给控制器​​。什么我想现在要做的,就是创建一个 ActionLink的具有查询字符串与上述格式的作品。

This works fine when using my View. I can set the properties of Model.Filter and they are passed to the Controller. What I am trying to do now, is create an ActionLink that has a query string that works with the above format.

从上面我查看生成的查询字符串看起来是这样的:

The query string generated by my View from above looks like this:

http://localhost:51050/?Filter.MessageFilter=Stuff&Filter.OtherProp=MoreStuff

我需要在不同的视图用于在去上面的查看网格每一行生成一个ActionLink的。

I need to generate an ActionLink in a different View for each row in a grid that goes to the View above.

我曾尝试:

Html.ActionLink(
    item.Message,
    "Index",
    "Home",
    new { Filter = new { MessageFilter = item.Message, }, },
    null);

我也试过 routeValues​​ 参数设置为:

new MyViewModel { Filter = new FilterViewModel { MessageFilter = item.Message, }, },

但是,这些不生成查询字符串像的上述一个

But these do not generate the query string like the above one.

推荐答案

您可以从 FilterViewModel 实例创建一个RouteValueDictionary,然后使用 ToDictionary 上传递给另一个RouteValues​​与 pfixed所有键$ p $过滤器。

You could create one RouteValueDictionary from a FilterViewModel instance and then use ToDictionary on that to pass to another RouteValues with all the keys prefixed with 'Filter.'.

进一步考虑它,你可以构建 RouteValueDictionary 的一个特殊的覆盖,它接受一个preFIX(因此使其成为其他方案的更多有用):

Taking it further, you could construct a special override of RouteValueDictionary which accepts a prefix (therefore making it more useful for other scenarios):

public class PrefixedRouteValueDictionary : RouteValueDictionary
{
  public PrefixedRouteValueDictionary(string prefix, object o)
    : this(prefix, new RouteValueDictionary(o))
  { }

  public PrefixedRouteValueDictionary(string prefix, IDictionary<string, object> d)
    : base(d.ToDictionary(kvp=>(prefix ?? "") + kvp.Key, kvp => kvp.Value))
  { }
}

使用,你现在可以做的:

With that you can now do:

Html.ActionLink( 
  item.Message, 
  "Index", 
  "Home", 
  new PrefixedRouteValueDictionary("Filter.", 
    new FilterViewModel() { MessageFilter = item.Message }), 
  null); 

需要说明的这一点,虽然是在添加删除 TryGetValue 此[字符串键] 方法不会改变,考虑到了 preFIX 。这可以通过定义来实现这些方法的新的版本,而是因为他们不是虚拟的,他们会只从知道他们在说一个<$呼叫者工作C $ C> prefixedRouteValueDictionary ,而不是 RouteValueDictionary

The caveat to this, though, is that the Add, Remove, TryGetValue and this[string key] methods aren't altered to take into account the prefix. That can be achieved by defining new versions of those methods, but because they're not virtual, they'd only work from callers that know they're talking to a PrefixedRouteValueDictionary instead of a RouteValueDictionary.

这篇关于如何创建与视图模型属性的ActionLink的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆