Home Adding dynamic field names to MVC Razor
Reply: 0

Adding dynamic field names to MVC Razor

dacc
1#
dacc Published in 2018-02-11 04:51:41Z

I have a viewmodel which has fields like

public decimal? V1 { get; set; }
public decimal? V2 { get; set; }
public decimal? V3 { get; set; }
public decimal? V4 { get; set; }
public decimal? V5 { get; set; }
public decimal? V6 { get; set; }
public decimal? V7 { get; set; }
public decimal? V8 { get; set; }

Now we only want to show these fields in MVC Razor view based on a loop condition as in-- (if its week 1 show only V1 (html.textboxfor)

Is there any way I can add this condition to razor and loop through the field names like this Model.V[1] or anything as long as it shows the desired result

i have tried reflection scheme

@for (int k = firstWeek; k <= lastweek; k++)
{
    var col = "V" + k;

    var fieldName= Model.GetType().GetProperty(col);
}

but I don't know how to assign the fieldName to @Html.TextBoxFor(model=>).

Any help or ideas please?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306245 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO