In my ASP.Net MVC appication I have a loop which i'd like to display different Properties values in HTML using the HTML.DisplayFor method. But it will not accept a string value to direct to the Model parameter.
Normal mode:
<img src="@Html.DisplayFor(model => model.Image_1)" />
My loop:
@for (int i = 1; i < 11; i++)
{
string currentImage = "Model.Image_" + i;
if ((@Html.DisplayFor(model => "Model.Image_" + i ).ToString()) != "")
{
<div>
<img src="@Html.DisplayFor(model => currentImage)" />
</div>
}
}
My results in the img src are just currentImage.
I'd like it to be Model.Image_" + i.
How would I do that?
If you have a better way of doing this that would be appreciated as well. Should my images have their own Model -class, you think?
ifblock (that makes no sense) and just usestring currentImage = "Image_" + i; <img src="@Html.Display(currentImage)" />but all this suggests a design problem. You should have aIEnumerable<string> Imagesproperty containing the image pathsImage_1,Image_2etc