Solved

Is it possible to put a variable in the field name in a Lamda expression

Posted on 2015-01-07
6
100 Views
Last Modified: 2015-01-09
Hi again,
I have 4 fields in a record Agent1, Agent2, Agent3, Agent4

In my Viewmodel I have getAgent1, getAgent2, getAgent3, getAgent4

Is there away instead of writing the code to get the data like
@Html.TexBoxFor(model => model.getAgent1, new{ @class = "AgentName})
more code
more code

Open in new window



I could do something like
int I = 0
While (Model.Agent[I].AgentCode != null)
{
@Html.TextBoxFor(model => model.getAgent[I], new{ @class="AgentName})
}

Open in new window

I know the (I) won't work because it is not an array I just want to put the number beside the getAgent

I have everything else working except the getAgent(I)
Thank you for any help.
0
Comment
Question by:Niall292
  • 4
6 Comments
 
LVL 9

Accepted Solution

by:
sudheeshthegreat earned 250 total points
ID: 40537259
You simply need to use a for loop to iterate over the items in the model, and use Html.TextBoxFor within the loop.

Have you seen this?
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

Sample:
<% for (int i = 0; i < 3; i++) { %>
  <%: Html.TextBoxFor(m => m[i].Title) %>
<% } %>

Open in new window

0
 

Author Comment

by:Niall292
ID: 40538203
That is the way I am going now, it wasn't the way I was thinking at the start.

Originally I had 4 textboxes in each record Agent1, Agent2, Agent3, Agent4 and was looking just to change the name of the field model.Agenti but I don't think that is possible.
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 250 total points
ID: 40538265
Programming should deal only with three possible quantities: 0, 1, several.
Any other quantity is a clue that something is wrong. Why 4 Agents? Why not 3 or 5?
Changing the number of agents should have no impact on your model.
You should be returning a list or an array.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Niall292
ID: 40538536
@louisfr, I am starting to understand that and as I said I am rewriting so they take a unlimited array
0
 

Author Comment

by:Niall292
ID: 40540724
I have rewriting nearly all my Creates, Edits, Lists, and details, so they are not expecting 4 agents but a unfixed number in a  list and it works a lot better but now I have a new hurdle with getting jquery to work when I press "add" to which should show a new row with all the text boxes to add new info.

but that is another question.
This line actually made me think in a whole new way
"Programming should deal only with three possible quantities: 0, 1, several."
0
 

Author Closing Comment

by:Niall292
ID: 40540728
Both comments made me think in a different way and sudheshthegreats comment gave me an example of the array
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

785 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