从自定义模型绑定器的controllerContext中获取FormCollection

发布于 2024-08-06 07:03:17 字数 167 浏览 8 评论 0原文

我有一个很好的函数来获取我的 FormCollection(从控制器提供)。现在我想做一个模型绑定,并让我的模型绑定器调用该函数,它需要 FormCollection。由于某种原因我能找到它。我以为会是这样 controllerContext.HttpContext.Request.Form

I had a nice function that took my FormCollection (provided from the controller). Now I want to do a model bind instead and have my model binder call that function and it needs the FormCollection. For some reason I can find it. I thought it would have been
controllerContext.HttpContext.Request.Form

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

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

发布评论

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

评论(3

↘人皮目录ツ 2024-08-13 07:03:17

试试这个:

var formCollection = new FormCollection(controllerContext.HttpContext.Request.Form)

FormCollection 是我们添加到 ASP.NET MVC 中的一种类型,它有自己的 ModelBinder。您可以查看 FormCollectionBinderAttribute 的代码来了解我的意思。

Try this:

var formCollection = new FormCollection(controllerContext.HttpContext.Request.Form)

FormCollection is a type we added to ASP.NET MVC that has its own ModelBinder. You can look at the code for FormCollectionBinderAttribute to see what I mean.

孤独陪着我 2024-08-13 07:03:17

直接访问表单集合似乎是不受欢迎的。以下是来自 MVC4 项目的示例,其中我有一个自定义 Razor EditorTemplate,它在单独的表单字段中捕获日期和时间。自定义绑定器检索各个字段的值并将它们组合成一个DateTime

public class DateTimeModelBinder : DefaultModelBinder
{
    private static readonly string DATE = "Date";
    private static readonly string TIME = "Time";
    private static readonly string DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm";
    public DateTimeModelBinder() { }

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext == null) throw new ArgumentNullException("bindingContext");

        var provider = new FormValueProvider(controllerContext);
        var keys = provider.GetKeysFromPrefix(bindingContext.ModelName);
        if (keys.Count == 2 && keys.ContainsKey(DATE) && keys.ContainsKey(TIME))
        {
            var date = provider.GetValue(string.Format("{0}.{1}", bindingContext.ModelName, DATE)).AttemptedValue;
            var time = provider.GetValue(string.Format("{0}.{1}", bindingContext.ModelName, TIME)).AttemptedValue;
            if (!string.IsNullOrWhiteSpace(date) && !string.IsNullOrWhiteSpace(time))
            {
                DateTime dt;
                if (DateTime.TryParseExact(string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} {1}", date, time),
                                            DATE_TIME_FORMAT,
                                            System.Globalization.CultureInfo.CurrentCulture,
                                            System.Globalization.DateTimeStyles.AssumeLocal,
                                            out dt))
                    return dt;
            }
        }

        return base.BindModel(controllerContext, bindingContext);
    }
}

Accessing the form collection directly appears to be frowned on. The following is an example from an MVC4 project where I have a custom Razor EditorTemplate that captures the date and time in separate form fields. The custom binder retrieves the values of the individual fields and combines them into a DateTime.

public class DateTimeModelBinder : DefaultModelBinder
{
    private static readonly string DATE = "Date";
    private static readonly string TIME = "Time";
    private static readonly string DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm";
    public DateTimeModelBinder() { }

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext == null) throw new ArgumentNullException("bindingContext");

        var provider = new FormValueProvider(controllerContext);
        var keys = provider.GetKeysFromPrefix(bindingContext.ModelName);
        if (keys.Count == 2 && keys.ContainsKey(DATE) && keys.ContainsKey(TIME))
        {
            var date = provider.GetValue(string.Format("{0}.{1}", bindingContext.ModelName, DATE)).AttemptedValue;
            var time = provider.GetValue(string.Format("{0}.{1}", bindingContext.ModelName, TIME)).AttemptedValue;
            if (!string.IsNullOrWhiteSpace(date) && !string.IsNullOrWhiteSpace(time))
            {
                DateTime dt;
                if (DateTime.TryParseExact(string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} {1}", date, time),
                                            DATE_TIME_FORMAT,
                                            System.Globalization.CultureInfo.CurrentCulture,
                                            System.Globalization.DateTimeStyles.AssumeLocal,
                                            out dt))
                    return dt;
            }
        }

        return base.BindModel(controllerContext, bindingContext);
    }
}
夏见 2024-08-13 07:03:17

使用bingdingContext.ValueProvider(和bingdingContext.ValueProvider.TryGetValue等)直接获取值。

Use bindingContext.ValueProvider (and bindingContext.ValueProvider.TryGetValue, etc.) to get values directly.

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