Solved

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

Posted on 2015-01-07
6
104 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
[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
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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