开发者

ASP.NET MVC Controller parameters create object automatically

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开发者_如何学C 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.


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.


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);


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" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜