Solved

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

Posted on 2015-01-07
6
103 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

726 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