3

I'm fairly new to C#, and am creating my first MVC project, and am having a hard time figuring out the methodology of passing 3 parameters of varying types to a controller action. Here is my controller method:

public ActionResult Create(Notification notification, string hash, list<int> users){
    //code inside method irrelevant...
}

and my Notification model:

public class Notification
{
    public int ID { get; set; }
    public string ApplicationID { get; set; }
    public string Description { get; set; }
    public System.DateTime DateStamp { get; set; }
}

Before I added the List<> parameter it worked fine by having the posted data (or querystring) like so:

ApplicationID=1&Description=yo&hash=abcdefg 

And it magically knew that the two parameters ('ApplicationID' and 'Description') belonged to the notification object. But now I'd like to add in a list<> of ints.

Is this something that can be done and how would you format the data/querystring passed?

BadFeelingAboutThis
  • 14,353
  • 2
  • 31
  • 39
  • [This post](http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx) may help – Zaid Masud Aug 24 '12 at 21:40
  • Thanks OP and @Darin Dimitrov! I used this question and sample code as a basis for a similar question I had: http://stackoverflow.com/questions/20915681/how-to-pass-a-complex-view-model-into-a-controller-action-via-ajax-call-with-jso – longda Jan 04 '14 at 01:32

1 Answers1

3

Is this something that can be done

Yes.

and how would you format the data/querystring passed?

Like this:

ApplicationID=1&Description=yo&hash=abcdefg&users=1&users=2&users=3

or if you prefer like this:

ApplicationID=1&Description=yo&hash=abcdefg&users[0]=1&users[1]=2&users[2]=3

Also you might find the following blog post an useful read.

But before transforming your controller action signatures into some spaghettified code and readers of your code having to cycle a couple of screens horizontally in order to see the millions of parameters this action takes, stop the madness and introduce a view model:

public class CreateViewModel
{
    public Notification Notification { get; set; }
    public string Hash { get; set; }
    public List<int> Users { get; set; }
}

and then:

public ActionResult Create(CreateViewModel model)
{
    //code inside method irrelevant...
}

and then:

notification.applicationID=1&notification.description=yo&hash=abcdefg&users[0]=1&users[1]=2&users[2]=3
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • Really silly question: is there an easy way to use this with a custom model binder? Imagine we were sending in polymorphic objects for Notfication... say an AlertNotification, WarningNotification, or ErrorNotification which all inherit from Notification. Oh, another assumption would be using an ajax call posting JSON data rather than query string notation. – longda Jan 03 '14 at 23:55
  • 1
    @longda, if you intend to use JSON, then you'd rather use some custom JSON serializer/converter to handle the polymorphic objects. – Darin Dimitrov Jan 04 '14 at 09:42