Solved

How to put class(es) into list before sending to controller

Posted on 2015-01-05
11
108 Views
Last Modified: 2015-01-05
Hi all,
I have a Create view which I am using a ViewModel to send data to 2 classes.

to give a brief example it gives the name of a product, Product ID, Application Date, Approved Date, etc etc and these field go to Class1
but then there could be up to 4 sales men
Sales Man, commission
Sales Man2, Commission
Sales Man 3, Commission
Sales Man 4, Commission

Then I have a Save button at the bottom of the page which goes to the controller it keeps failing but when I debug it, it seems only one Salesman is going to the controller,. Can I put them all in a List before it goes to the controller.
Any help is greatly appreciated
0
Comment
Question by:Niall292
  • 5
  • 5
11 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40532305
Yes. MVC's model binding allows for a special syntax when submitting data to the server. Whenever you have a list, you can use and array-like notation to indicate to the model binder that a list is forthcoming. For your example above, you might name your inputs as such:

<label for="salesman1">Sales Man 1</label>
<input id="salesman1" name="actionParameterName[0].Name" />
<label for="commission1">Commission 1</label>
<input id="commission1" name="actionParameterName[0].Commission" />

<label for="salesman2">Sales Man 2</label>
<input id="salesman2" name="actionParameterName[1].Name" />
<label for="commission2">Commission  2</label>
<input id="commission2" name="actionParameterName[1].Commission" />

... etc.

Open in new window


Take note of the square brackets usage on the <input> names. This will tell the model binder that it should map into an array/list.

This explanation isn't very spectacular with such a limited example, so the following article goes into a tad greater detail:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40532313
Please confirm that this is a MVC project (Also specify  MVC/VS version) and add you current code so far.
Also if you could confirm that that task is to pass a list to the view as shown here and save that list back using the save button.
0
 

Author Comment

by:Niall292
ID: 40532338
@Kaufmed, at first glance your example makes a lot of sense, I will try it tomorrow

@Miguel,
Yes it is a VS 2012 MVC project and posting data from View, saving it to the server using the Save button.

Thanks
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40532382
NP. Just to add a bit more detail, you use the brackets on the thing that is a collection. This can be one or more things in your hierarchy. If you have nested collections, you can apply the same syntax to each level. So a class such as:

public class Person
{
    public string Name { get; set; }
    public IList<Address> Addresses { get; set; }
}

public class Address
{
    public IList<string> Street { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

Open in new window


...and a controller like:

public ActionResult UpdatePerson(Person p)
{
    // TODO: add logic
}

Open in new window


...could be represented in HTML as:

<input name="p.Name" />
<input name="p.Addresses[0].Street[0]" /> <!-- Street Number -->
<input name="p.Addresses[0].Street[1]" /> <!-- Apartment Number -->
<input name="p.Addresses[0].State" />
<input name="p.Addresses[0].Zip" />

Open in new window

0
 

Author Comment

by:Niall292
ID: 40532401
Could I use TextBoxFor
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40532447
I believe you can, but I'd have to play around with it. I think you'd have to use one of the overloads that takes in an anonymous object which allows you to define custom attributes on the generated element.
0
 

Author Comment

by:Niall292
ID: 40532489
My life is so exciting that I had to log back into work and try it straight away.
Is there a way I can put the product ID to all the Salesmen in one go or do I have to do it 4 times
and I think it is working with the TextBoxFor
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40532494
Since you're working with a list, I believe you'll need to do it separately. You could do some trickery in the C# code, but IMO this may not be the most easy-to-understand approach.
0
 

Author Comment

by:Niall292
ID: 40532643
Kaufmed,
Thank you this worked great, I am still having issues because the records are not getting saved to the tables but this is a totally different issue.

Thanks again
0
 

Author Closing Comment

by:Niall292
ID: 40532646
This was a brilliant solution to my problem
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40532705
NP. Glad to help  = )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now