如何在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,我没有任何后面的代码,因此我无法像以前一样访问图像,并且需要将其传递给 .aspx 文件。
这是怎么做到的?
谢谢 中号
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将使用强类型视图并将模型从控制器传递到视图中。
您可以在此处找到一些详细信息。
然后,您将使用类似...
您的控制器将如下所示,您可以在其中从某些外部源获取图像列表。
在上面的代码中,您可以使用下面的代码来渲染视图,
尽管我更喜欢通过下面的调用来明确显示,并指定要渲染的视图的名称(尽管它与当前控制器操作的名称相同,但我认为读起来更好)
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...
Your controller would look something like below, where you may get a list of images from some external source.
In the above code you could just use the below to render the view
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)