Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-07
6
Medium Priority
?
107 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 1000 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 1000 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

773 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