Asp.net mvc 3 动态表单生成和传递查询参数

Asp.net mvc 3 动态表单生成和传递查询参数,asp.net-mvc-3,oauth,query-parameters,formcollection,formbuilder,Asp.net Mvc 3,Oauth,Query Parameters,Formcollection,Formbuilder,我正在开发一个基于数据库中的一些元数据表动态生成的表单。我创建了输入标签,名字像setting_1、setting_53、setting_22,其中数字是元数据的主键。因为内容是动态的,所以我使用FormCollection作为POST请求的唯一参数 问题1:GET请求是否有类似FormCollection的类?我想要直接访问查询参数 问题2:如果我需要传递这些查询参数,是否有一种简单/安全的方法来构建我的URL 我最大的担忧之一是,一些设置是通过OAuth填充的,因此用户将被重定向到外部页面。

我正在开发一个基于数据库中的一些元数据表动态生成的表单。我创建了输入标签,名字像setting_1、setting_53、setting_22,其中数字是元数据的主键。因为内容是动态的,所以我使用FormCollection作为POST请求的唯一参数

问题1:GET请求是否有类似FormCollection的类?我想要直接访问查询参数

问题2:如果我需要传递这些查询参数,是否有一种简单/安全的方法来构建我的URL

我最大的担忧之一是,一些设置是通过OAuth填充的,因此用户将被重定向到外部页面。我必须将查询字符串作为“状态”传递,一旦用户返回,我就需要恢复该状态。我需要使用此状态来选择用户在表单输入过程中离开的位置。这就是为什么我需要一个非常简单的机制来传递查询参数的原因


有人处理过这样的动态页面吗?有没有好的模式和实践来传递这些页面?

当然,您可以查看控制器操作内部的
Request.QueryString

但如果是我做的话,我会写一个定制的模型活页夹

这是一个示例模型活页夹。我还没有测试过这个

public class MyModelBinder: DefaultModelBinder
{
    private static void BindSettingProperty(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        PropertyDescriptor propertyDescriptor)
    {
        if (propertyDescriptor.PropertyType != typeof(IDictionary<string, string>))
        {
            throw new InvalidOperationException("This binder is for setting dictionaries only.");
        }
        var originalValue = propertyDescriptor.GetValue(bindingContext.Model) as IDictionary<string, string>;
        var value = originalValue ?? new Dictionary<string, string>();
        var settingKeys = controllerContext.HttpContext.Request.QueryString.AllKeys.Where(k => k.StartsWith("setting_", StringComparison.OrdinalIgnoreCase));
        foreach (var settingKey in settingKeys)
        {
            var key = settingKey.Substring(8);
            value.Add(key, bindingContext.ValueProvider.GetValue(settingKey).AttemptedValue);
        }
        if (value.Any() && (originalValue == null))
        {
            propertyDescriptor.SetValue(bindingContext.Model, value);
        }
    }

    protected override void BindProperty(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        PropertyDescriptor propertyDescriptor)
    {
        if (propertyDescriptor.Name.StartsWith("setting_", StringComparison.OrdinalIgnoreCase)
        {
            BindSettingProperty(controllerContext, bindingContext, propertyDescriptor);
        }
        else
        {
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
}
公共类MyModelBinder:DefaultModelBinder
{
私有静态void BindSettingProperty(
ControllerContext ControllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor PropertyDescriptor)
{
if(propertyDescriptor.PropertyType!=typeof(IDictionary))
{
抛出新的InvalidOperationException(“此活页夹仅用于设置字典。”);
}
var originalValue=propertyDescriptor.GetValue(bindingContext.Model)作为IDictionary;
var值=原始值??新字典();
var settingKeys=controllerContext.HttpContext.Request.QueryString.AllKeys.Where(k=>k.StartsWith(“setting”,StringComparison.OrdinalIgnoreCase));
foreach(设置键中的var设置键)
{
变量键=设置键。子串(8);
Add(key,bindingContext.ValueProvider.GetValue(settingKey.AttemptedValue);
}
if(value.Any()&&(originalValue==null))
{
propertyDescriptor.SetValue(bindingContext.Model,value);
}
}
受保护的覆盖无效绑定属性(
ControllerContext ControllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor PropertyDescriptor)
{
if(propertyDescriptor.Name.StartWith(“设置”,StringComparison.OrdinalIgnoreCase)
{
BindSettingProperty(controllerContext、bindingContext、propertyDescriptor);
}
其他的
{
BindProperty(controllerContext、bindingContext、propertyDescriptor);
}
}
}

您是否有构建自定义模型绑定的链接?这种方法允许我报告错误吗?我已经在手动构建对象,因此模型绑定不需要太多工作。我添加了一个示例。使用时请自行承担风险!