Christian Gollhardt
Christian Gollhardt

Reputation: 17014

Pass model to RouteValueCollection

I have this model:

public class ReportConfiguration
{
    public DateTime ReportStart { get; set; }

    public DateTime ReportEnd { get; set; }
}

Is it possible to pass both values via RouteValueCollection to the controller?

I would like to have this method signature in my controller:

public ActionResult Read_Reports([DataSourceRequest]DataSourceRequest request, ReportConfiguration config);

I tried this:

new { area = "ReportCashRegister", config = MyInctanceOfReportConfiguration }

But it is resulting in a querystring variable like config=Namespace.ReportConfiguration instead of ReportConfiguration.ReportStart=value and ReportConfiguration.ReportEnd=value

I want to avoid to copy each property into the method signature like this: (Because my Model is an example and has much more properties I need to copy)

public ActionResult Read_Reports([DataSourceRequest]DataSourceRequest request, DateTime reportStart, DateTime reportEnd);

Upvotes: 0

Views: 68

Answers (1)

Nkosi
Nkosi

Reputation: 247143

Action

public ActionResult Read_Reports([DataSourceRequest]DataSourceRequest request, [FromBody]ReportConfiguration config);

In View

new { area = "ReportCashRegister", ReportStart = @DateTime.Now, ReportEnd = @DateTime.Now }

It will add those values to the RouteValueDictionary and bind them to the model/parameter in the action.

Upvotes: 1

Related Questions