繁体   English   中英

如何在asp.net-MVC2中传递列表

[英]How to pass a list in asp.net-MVC2

使用asp.net-4.0,我这样做:

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>

然后,像下面的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("", "",  "", "", "", "");

现在有了asp.net-MVC2,我没有任何代码在后面,因此我无法像以前那样访问Images,而是需要如何将其传递到.aspx文件。

怎么做?

谢谢M

您将使用强类型视图,并将模型从控制器传递到视图中。

您可以在此处找到一些详细信息。

然后,您将使用类似...

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

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

<% } %>

您的控制器如下所示,您可以从某些外部来源获取图像列表。

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

  return View ("Index", imageViewModel);    

}

在上面的代码中,您可以仅使用以下内容渲染视图

return View (imageViewModel);  

我更喜欢在下面的调用中明确显示,并指​​定要渲染的视图的名称(尽管它与当前控制器动作的名称相同,但我认为它读起来更好)

return View ("Index", imageViewModel); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM