使用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我没有任何代码,所以我不能像以前一样访问图像,而是需要将它传递给.aspx文件。
这是怎么做到的?
由于 M
答案 0 :(得分:5)
您将使用强类型视图并将模型从控制器传递到视图中。
您可以找到一些详细信息here。
然后您将使用类似......
的内容<% 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);