简体   繁体   English

在ASP.NET MVC 3中以编程方式创建routeValues(匿名类型)

[英]Create routeValues (anonymous type) programmatically in ASP.NET MVC 3

Assume we have a page that include some elements to support search from DB, so in Post Action we need to find which elements are active and according to that create route values something like this: 假设我们有一个页面,其中包含一些支持从DB搜索的元素,因此在Post Action中我们需要找到哪些元素是活动的,并根据这些创建路由值,如下所示:

List<Parameter> SearchParameters = GetFilterParameters(collection);


if(SearchParameters.Count > 0)
foreach(Parameter item in SearchParameters) {

    switch(item.Name) {
                    case "Category":
                    CategoryValue= item.Value;
                        break;

                    case "StartDate":
                    StartDateValue= item.Value;
                        break;


                    case "Product":
                    ProductValue= item.Value;
                        break;

                }
return RedirectToAction("Index", new {category = CategoryValue, startdate=StartDateValue, product=ProductValue });

So is there any way to define routeValues dynamically something like the followings Pseudo-Code: 那么有没有办法动态定义routeValues类似于以下Pseudo-Code:

var dynamicRoutValues;
foreach(Parameter item in SearchParameters) {

  dynamicRoutValues.Add(item.Name, item.Value)

}

    return RedirectToAction("Index", dynamicRoutValues);

You can use a RouteValueDictionary : 您可以使用RouteValueDictionary

var dynamicRoutValues = new RouteValueDictionary();
foreach(Parameter item in SearchParameters) {
    dynamicRoutValues[item.Name] = item.Value;
}

return RedirectToAction("Index", dynamicRoutValues);

If SearchParameters implemented IDictionary<string, object> then you could pass it directly to the RouteValueDictionary constructor: 如果SearchParameters实现了IDictionary<string, object>那么您可以将它直接传递给RouteValueDictionary构造函数:

return RedirectToAction("Index", new RouteValueDictionary(SearchParameters));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动将匿名属性添加到routeValues中,该属性是ASP.NET MVC4 ActionLink中的对象 - Add automatically anonymous properties to routeValues which is object in ASP.NET MVC4 ActionLink ASP.NET MVC中的匿名类型语法 - Anonymous Type syntax in ASP.NET MVC ASP.NET MVC - 将 JavaScriptStringEncode() 与匿名类型和 Jquery 一起使用 - ASP.NET MVC - Using JavaScriptStringEncode() with anonymous Type and Jquery 如何以编程方式创建ASP.NET MVC项目? - How to programmatically create an ASP.NET MVC project? ASP.NET MVC - 在Html.ActionLink routeValues中传递模型的麻烦 - ASP.NET MVC - Trouble passing model in Html.ActionLink routeValues 如何在asp.net mvc 5中通过actionlink传递包含连字符的routeValues - How to pass routeValues that contains hyphen via actionlink in asp.net mvc 5 在ASP.NET MVC中传递匿名类型 - Passing anonymous types in ASP.NET MVC 将匿名用户或错误的用户类型重定向到 ASP.net MVC 3 中特定控制器内的登录页面 - Redirecting Anonymous User or wrong user type to login page within a specific controller in ASP.net MVC 3 为什么在加载视图和单击ASP.NET MVC中的Html.ActionLink之间,我的routeValues变量会发生变化? - Why is my routeValues variable changing between my view loading and me clicking on my Html.ActionLink in ASP.NET MVC? 在ASP.NET中以编程方式创建Listview - Create Listview programmatically in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM