Reputation: 14406
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?
Upvotes: 3
Views: 493
Reputation: 1039100
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¬ification.description=yo&hash=abcdefg&users[0]=1&users[1]=2&users[2]=3
Upvotes: 3