1

With asp.net-4.0 I did this:

slideshow.aspx
<div class="wrapCarousel">  
    <div class="Carousel">  
       <% foreach(var image in Images) { %>
       <div class="placeImages">
        <img width="150px" height="150px" src="../Img/<%=image.TnImg%>" alt="<%=image.Name%>" />
        <div class="imageText">   
         <%=image.Name%>
        </div>
       </div>
       <% } %>
   </div>

And then Images was in the code behind like this slideshow.aspx.cs:

    public class Image
    {
        public string TnImg { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string RefPlace { get; set; }
        public string RefInfo { get; set; }
        public string RefInfoDynamic { get; set; }

        public Image(string TnImg, string Name, string City, string RefPlace, string RefInfo, string RefInfoDynamic)
        {
            this.TnImg = TnImg;
            this.Name = Name;
            this.City = City;
            this.RefPlace = RefPlace;
            this.RefInfo = RefInfo;
            this.RefInfoDynamic = RefInfoDynamic;
        }
    }

     Images.Add(new Image("", "",  "", "", "", "");

Now with asp.net-MVC2 I don't have any code behind so I can't access Images like before and some how instead need to pass it to the .aspx file.

How is this done?

Thanks M

1 Answer 1

5

You would use a strongly typed view and pass the Model into the View from the controller.

You can find some details here.

You would then use something like...

<% foreach(var image in Model.Images) { %>

   <div><%= image.Name %></div>

<% } %>

Your controller would look something like below, where you may get a list of images from some external source.

public ActionResult Index()
{
  ImageViewModel imageViewModel = new ImageViewModel();
  imageViewModel.Images = _imageRepository.GetImages();

  return View ("Index", imageViewModel);    

}

In the above code you could just use the below to render the view

return View (imageViewModel);  

I prefer to be explicit though with the call below, and specify the name of the view to render ( Even though it is the same name of the current controller action, I think it reads better)

return View ("Index", imageViewModel); 
Sign up to request clarification or add additional context in comments.

6 Comments

So in this case what is ImageViewModel() and should the foreach be like this "foreach(var image in Model.imageViewModel)" then?
If you strongly type your view as the ImageViewModel type, and that type had a property Images, you would access the data in your View with Model.Images.
ImageViewModel is a class you create that has the Images property. In the view, you then refer to the passed-in instance of the model class as Model.
Okay, I see and GetImages(); is the function that returns the images that I want to have on my index.aspx? The problem is that I'm not sure of how to create "ImageViewModel is a class you create that has the Images property" as a strongly type
ImageViewModel would just be a class definition, you could create a new folder called ViewModels and keep all your viewmodels in there. The images property would likely return a List<Image>. In this case the GetImages method would return a List<Image> for you to populate the property with.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.