动态表单构建和传递查询参数

发布于 2024-12-28 01:17:34 字数 425 浏览 1 评论 0原文

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

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

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

我最关心的问题之一是某些设置是通过 OAuth 填充的,因此用户将被重定向到外部页面。我必须将查询字符串作为“状态”传递,一旦用户返回,我将需要恢复该状态。我需要使用此状态来继续用户在表单输入过程中停止的位置。更重要的是,我需要一个非常简单的机制来传递查询参数。

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

I am working on a form that is generated dynamically based on some meta-data tables in my database. I create input tags with names like setting_1, setting_53, setting_22 where the number is the primary key of the meta-data. Since the content is dynamic, I am using FormCollection as the sole parameter on POST requests.

Question 1: Is there a FormCollection-like class for GET requests? I want direct access to the query parameters.

Question 2: If I need to pass these query parameters around, is there an easy/safe way to build my URLs?

One of my big concerns is that some of the settings are populated via OAuth, so the user will be redirected to an external page. I will have to pass the query string as "state" which I will need to recover once the user returns. I will need to use this state to pick up where the user left off in the form entry process. All the more reason why I need a very fool-proof mechanism for passing query parameters around.

Has anyone dealt with dynamic pages like these? Are there good patterns and practices for passing these pages around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2025-01-04 01:17:34

好吧,您当然可以查看控制器操作内部的 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);
        }
    }
}

Well, you can certainly look at Request.QueryString inside of a controller action.

But if it were me doing it, I'd write a custom model binder instead.

Here's a sample model binder. I haven't tested this!

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文