Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-05
11
Medium Priority
?
117 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 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
Technology Partners: 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

926 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