Solved

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

Posted on 2015-01-07
6
98 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

20 Experts available now in Live!

Get 1:1 Help Now