MVC5 ViewModel binding in HTTP GET?

I have an action like this

public ActionResult Overview(TimeAxisVM TimeAxis = null)
{
    return View(new OverviewVM());
}

Show a model like this

public class TimeAxisVM
{
    // omitted ctor

    [DataType(DataType.DateTime)]
    public DateTime? From { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime? To { get; set; }
}

Editor Template for Presentation Model

@model TimeAxisVM

@using (Html.BeginForm("Overview", "Controller", FormMethod.Get))
{
    @Html.EditorFor(model => model.From)

    @Html.EditorFor(model => model.To)

    <button type="submit">Submit</button>
}

And a view for the Browse action like this

@model OverviewVM

@Html.EditorFor(model => model.TimeAxis)

When I execute the GET request, this is the query string TimeAxis.From = 22. + 02. + 2014 & TimeAxis.To = 25. + 02. + 2014, but once in action TimeAxis.From and TimeAxis.To are equal to zero.

If I change the form method to POST, it immediately works as expected. From a design point of view, this should / should be a GET request.

Any ideas on how to make model binding work for GET?

UPDATE:

Change action to

public ActionResult Overview(DateTime? From = null, DateTime? To = null)

and sending a request in this form: ... / Browse /? From = 22. + 02. + 2014 & To = 25. + 02. + 2014 also works.

, - EditorFor TimeAxis.From TimeAxis.To. ViewModel/form.

+4
2

, ModelView View,

return View(TimeAxis);

, , Controller. HomeController - , ?

, ,

@using (Html.BeginForm("Overview", "Home", FormMethod.Get))

, , Overview, Home


, :

public ActionResult Overview(TimeAxisVM TimeAxis)
{
    return View(TimeAxis);
}

@using (Html.BeginForm("Overview", "Home", FormMethod.Get))
{
    @Html.EditorFor(Model => Model.From)

    @Html.EditorFor(Model => Model.To)

    <button type="submit">Submit</button>
}

: http://screencast.com/t/7G6ofEq0vZEo

: http://ge.tt/1Uh80pK1/v/0?c

0

All Articles