Solved

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

Posted on 2015-01-05
11
105 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 74

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 74

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 74

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 74

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 74

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

11 Experts available now in Live!

Get 1:1 Help Now