![](/img/trans.png)
[英]Correct way to edit and update complex viewmodel objects using asp.net-mvc2 and entity framework
[英]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.