Shaik Mohammad Rafi
Shaik Mohammad Rafi

Reputation: 31

Binding list of Dates using specific culture in asp.net mvc 3 fails

i have this scenario where i need to accept three meeting schedules , below are the details

meeting Schedule model

public class MeetingSchedule
{
    public DateTime Date { get; set; }

}

Form looks like

<form action="@Url.Action("Schedule")" method="post" >
    <input type="text" name="meetingSchedules[1].Date" id="schedule2" class="datepicker" />
    <input type="text" name="meetingSchedules[2].Date" id="schedule3" class="datepicker" />
</form>

and Action

[HttpPost]
public ActionResult Schedule(List<MeetingSchedule> meetingSchedules)
    {}

i set the culture

<system.web>    
    <globalization uiCulture="en-GB" culture="en-GB" />
</system.web>

Still could not bind Date of format "dd/MM/yyyy", ex: if i choose any one date as 26/10/2011 , the model binder could not bind it , instead show default DateTime Value.

Please help me with this

Thanks

Upvotes: 0

Views: 312

Answers (1)

ssdeshmukh
ssdeshmukh

Reputation: 21

I ran into same issue and found out the problem is with the code.

If you think of action as a plain method, the model binder kicks in when the action method is invoked, prior to this if you have set the culture info then you are all set.

In the constructor add following code:

System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

Now try again, the model binder should now be able to recognize the date in dd/MM/yyyy format.

Upvotes: 2

Related Questions