简体   繁体   English

ASP.NET MVC3 自定义转换输入参数的最佳方式

[英]ASP.NET MVC3 Best way to customize converting input parameters

I'm going to add support of different timezone in my ASP.NET MVC3 app.我将在我的 ASP.NET MVC3 应用程序中添加对不同时区的支持。

The solution is to store date values in UTC time, and store user timezone offset.解决方案是以 UTC 时间存储日期值,并存储用户时区偏移量。

So, I need 2 things:所以,我需要两件事:

  1. Send date values to user in "user time" (it's clear, I pass date to wrapper that convert time to user time)在“用户时间”中向用户发送日期值(很明显,我将日期传递给将时间转换为用户时间的包装器)

  2. Retrieve date from user in "user time" and convert it to UTC.从“用户时间”中的用户检索日期并将其转换为 UTC。

Question is - what is the nice way to convert all input date values to UTC time.问题是 - 将所有输入日期值转换为 UTC 时间的好方法是什么。 Or is there a generic way to add some converting rules to ASP.NET MVC?或者是否有一种通用的方法可以将一些转换规则添加到 ASP.NET MVC?

This is ideal schema what I want (controller receive already converted values)这是我想要的理想模式(控制器接收已经转换的值)

在此处输入图像描述

A way could be to create your own ModelBinder that do the work of mapping and preparing the data for your action method in the controller.一种方法是创建您自己的 ModelBinder,为 controller 中的操作方法执行映射和准备数据的工作。

This post from Hanselman's Blog should give you an idea of what I mean. Hanselman 博客中的这篇文章应该能让您了解我的意思。 Obviously I'm assuming that you can get all the data you need inside the method显然我假设你可以在方法中获得你需要的所有数据

BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext);

Like getting the user timezone with a repository or a hidden field in the post.就像使用存储库或帖子中的隐藏字段获取用户时区一样。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM