Solved

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

Posted on 2015-01-05
11
114 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

631 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