Solved

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

Posted on 2015-01-05
11
112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 36

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net convert long time to mm:ss 23 20
C# Service FileSystemwatcher isse 9 46
How to identify a paragraph in the body of text? 2 49
Json and ajax 1 19
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 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