开发者

How to pass a list in asp.net-MVC2

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%&开发者_JAVA百科gt;
        </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


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); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜