If I have a custom object like this:
public class StatisticsRequest
{
public string Level { get; set; }
public string Analysis { get; set; }
...more properties
}
Then can I declare an MVC2 controller like this?:
public ActionResult GetResponseStats(StatisticsRequest statsRequest)
and get my querystring parameters automatically parsed into my custom object?
It's not working for me - can you do this?
Edit:
This is my entire controller class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tradeshow.Models;
namespace Tradeshow.Controllers
{
[Authorize]
public class DashboardController : Controller
{
public ActionResult GetResponseStats(StatisticsRequest statsRequest)// string profileid, string analysis, string question, string answer, string omitheaders)
{
Tradeshow.Models.Mongo mongo = new Models.Mongo();
// For top-level requests that don't specify the analysis, use the previously requested top-level analysis
if (statsRequest.IsTopLevelRequest)
{
if (statsRequest.Analysis == null || statsRequest.Analysis.Length == 0)
{
statsRequest.Analysis = (String)Session["statsanalysistype"];
}
else
{
Session["statsanalysistype"] = statsRequest.Analysis;
}
}
string clientdatabasename = (String)Session["clientdatabasename"];
Dashboard dashboard = mongo.BuildResponseDashboard(clientdatabasename,statsRequest);
return PartialView("ProfileDashboard",dashboard);
}
}
}
This is my entire StatisticsRequest object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Tradeshow.Models
{
/// <summary>
/// Encapsulates the properties that make up a statistics request for generating one or more graphs and charts
/// </summary>
public class StatisticsRequest
{
public string Level { get; set; }
public string Analysis { get; set; }
public string ProfileId { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
public string TimespanFormat { get; set; }
public string TimespanValue { get; set; }
public bool OmitHeaders
{
get
{
bool rc = false;
if (String.Compare(Level, "profile", true) == 0) rc = true;
return rc;
}
}
public bool IsTopLevelRequest
{
get
{
bool rc = false;
if (String.Compare(Level, "profile", true) == 0) rc = true;
return rc;
}
}
}
}
And the simplest test querystring (which fails) looks like this:
/Dashboard/GetResponseStats?profileid=123&unique=775765
A lot of the time only one or two of the parameters will be passed in the querystring.
Edit2
One other point - the StatisticsRequest object is just an arbitrary object, and has nothing to do with the View Model. I created the StatisticsRequest object purely to encapsulate the request, not to support any form-based views etc.