1

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.

3
  • What are the 'more properties'? Commented Jun 20, 2011 at 12:13
  • Yes, this will work. We'd need more info about your action, the model, and query string params to help you figure out what's going wrong. Commented Jun 20, 2011 at 12:14
  • @nikmd23 and NerdFury - I have edited the post to show more info. Thanks for your help. Commented Jun 20, 2011 at 12:23

3 Answers 3

2

If you call UpdateModel(statsRequest); the default model binder will fill in the data using query string and forms data if the properties match by name. You can also call TryUpdateModel(statsRequest); which will work only if all of the properties can be updated.

Sign up to request clarification or add additional context in comments.

4 Comments

does this not happen automatically? Do I actually have to call UpdateModel?
Default model binding does happen automatically. It's pretty powerful on its own but if something is even a little off it won't work. You can call those methods to force it to bind using whatever information is available in the request.
hmmm - that has fixed it. The things that are 'off' are that the cases don't match (Analysis v, analysis) and many properties are often missing - these don't seem that bad to cause it to fail?
@Journeyman: Casing shouldn't matter. In fact in my forms I use PascalCasing, while in the controllers I use camelCasing. The model binder handles that without any issues. There must be something else it doesn't like.
0

Assuming you are using primitive types, the model binder should be able to construct an object of that type from values passed in. The querystring parameter names need to match the property names. If this is from a form post, make sure that your inputs are named correctly.

The easiest way to do that is to use the helpers.

Html.TextboxFor(m => m.Level);

Comments

0

Nerd fury is correct. If you were to view source on his example you would get something like this.

<input type="text" name="StatisticsRequest.Level" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.