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

发布于 2024-10-03 18:48:54 字数 1377 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ繥欢鉨o。 2024-10-10 18:48:55

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

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

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

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

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文