Pass DateTime value to controller

Sep 27, 2012 at 8:08 AM

hi Again


How can i pass the selected Date back to the controller, i've tried different things, but it keeps passing null.


 public ActionResult SubmitNewArrangement(string Navn, string Ansvarlig, DateTime Arrangements_Dato, DateTime Tilmeldings_Frist, bool Familie_Arrangement, string Beskrivelse)


  @using (@Html.BeginForm("SubmitNewArrangement", "Admin" ))
            <h4>Opret Arrangement</h4>
                @Html.TextBox("Navn", "Skrive et navn...")
                <p>V�lg ansvarlig</p>
                @Html.DropDownList("Ansvarlig", ViewBag.Admins != null ? (IEnumerable<SelectListItem>)ViewBag.Admins : new List<SelectListItem> { new SelectListItem { Text = "Ingen Administratore oprettet", Value = "ingen" } })
                @{ var Arrangements_Dato = Html.DateTime("Arrangements_Dato", DateTime.Now, dateInCalendar: true); }
                    inLine: false,
                    calendarOptions: new CalendarOptions
                            ChangeYear = true,
                            ChangeMonth = true

                <p>Tilmeldings frist</p>
                @{ var Tilmeldings_Frist = Html.DateTime("Tilmeldings_Frist", DateTime.Now.AddYears(1), dateInCalendar: true); }
                    inLine: false,
                    calendarOptions: new CalendarOptions
                            ChangeYear = true,
                            ChangeMonth = true

                <p>Familie arrangement?</p>
                @Html.CheckBox("Familie_Arrangement", false)
                <br />
                <button type="submit" class="btn btn-small btn-info">Opret</button>

Sep 27, 2012 at 2:41 PM
Edited Sep 27, 2012 at 2:42 PM

Just use a ViewModel in the receiving Action method instead of several parameters. Mvc Controls Toolkit controls uses customs binders and some of them doesnt work with named parameters of the action method and works just as properties of a ViewModel, because the custom model binding framework used to implement all controls is based on the concept of model transformation. So as a general rule it is better to Use A ViewModel for all HttpPost action methods. You can use parameters with the HttpGet action methods that takes their inputs from the Url query string.


I advice also using a Model for your view instead of using stuffs like:



So you can write Html.DateTimeFor(m => m.Arrengements_Dato)....

This way you avois errors and if you reegineer you cose changing names VS 2010 is able to change automatically the occurrence in m => m..... but it is not able to chenge names that are put into strings.


If the ViewModel of the receiving Action method is different from the one of the View you can use the CorssHelper. It creates a new Html object that is specific for any class you pass it, so you gan have an Html helper for the ViewModel of the receiving Action Method that created the m => m. expressions that you need to give the right names to the fields to be posted.

Sep 28, 2012 at 7:29 AM


Thanks for your help :-)