I got a business layer which returns an anonymous object which has a collection of rows which have dates. I want to get regional formatting of the dates, but I don't want to pass the regional setting into the business layer, instead keeping the formatting in the Controller Action of the MVC website.
Biz Layer:
public object GetItems(int catID)
{
var data = GetDbItems(catId).ToList();
var Items = new
{
total = data.Count(),
page = 1,
rows = (from c in data
select new {
ID = c.id,
Desc = c.desc,
CreationDate = c.CreationDate
})
};
return Items;
}
back in the Action Method of the controller I want to format the date in the collection inside of the anonymous type. How can I do this?
public JsonResult GetItems(int catID)
{
string cultureString = HttpContext.Request.UserLanguages.FirstOrDefault();
DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture(cultureString).DateTimeFormat;
var items = bizLayer.GetItems(catID);
var test = from i in items
select i.rows; // ???? doesn't know about this collection yet.
}
What I want to do.
get at the rows collection of the anonymous type, find the date column and format it with
CreateDate = c.CreateDate.ToString("d", dtfi)
I got a business layer which returns an anonymous object
That's probably the first thing that needs to be fixed and replace this anonymous object by a strongly typed model which would greatly simplify the mapping of this domain model to a view model in order to perform the desired formatting.
You can't. You need to make a concrete class with the same properties as your anonymous object(s) and pass back a YourNewClass
instead of an object
.
Why the business layer returns an anonymous object is certainly vague, you should fix that first. But if you can't the best shot you've got is parsing each element as a DateTime; and then reusing that information for the rest of the rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.