crazysnake
crazysnake

Reputation: 481

DateTime POSTed to controller turns null in azure while works in development environment

I encountered an odd problem in my MVC5 web app, that occurs only in azure and I was hoping someone here can help me out.

I have a form with fields FromDate and ToDate, which are POSTed to a controller. Dates are formatted to "dd.MM.yyyy". When I run the code in Visual Studio I get the correct values in the controller, but when deployed in azure the controller receives values 01.01.0001, which is the default date when a datetime object appears null.

I think the problem may be that my date format is not recognized as a datetime and they are replaced with default values instead of producing an error. I have to keep the date format in the ui and I think scripting the date format at clientside before POSTing is not the correct way to go so I'm looking for better options, preferably a fix to the problem instead of just avoiding the problem by sending strings and parsing them on server side.

Here's my clientside form using razor:

@using (Html.BeginForm())
{
    <p>
        <input type="text" id="FromDate" name="FromDate" value="@(Model.StartDate.ToString("dd.MM.yyyy"))">
        <input type="text" id="ToDate" name="ToDate" value="@(Model.EndDate.ToString("dd.MM.yyyy"))">
    </p>

    <input type="submit" value="Submit" />
}

Here's the controller:

[HttpPost]
public ActionResult MyController(MyInputObject input)
{
    // Do something with input.FromDate and input.ToDate
}

And this is my MyInputObject:

public class MyInputObject
{
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
}

Upvotes: 2

Views: 347

Answers (1)

crazysnake
crazysnake

Reputation: 481

I think I found it, the application's culture can be set in web.config under element like this:

<globalization enableClientBasedCulture="true" uiCulture="fi-FI" culture="fi-FI"/>

It does not answer how multicultural web apps are made but solves my problem.

Thanks Gaurav Mantri and Jason Evans for your comments, they got me on the right track!

Upvotes: 1

Related Questions