我在MVC 4视觉工作室工作。我想要做的是从我的数据库中选择所有图像并在我的视图中显示所有图像。
在我的数据库中,我有3列: id | FileTitle |文件路径
图像位于文件夹中。
我把它放在下拉列表中,当我选择值时,请显示图像。
但我的问题是,如何在列表中同时显示所有图像?
这是我的代码:
型号:
public class Image
{
public SelectList ImageList { get; set; }
public Image()
{
ImageList = GetImages();
}
public SelectList GetImages()
{
var list = new List<SelectListItem>();
string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var con = new SqlConnection(connection))
{
con.Open();
using (var command = new SqlCommand("SELECT * FROM myimage", con))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string title = reader[1] as string;
string imagePath = reader[2] as string;
list.Add(new SelectListItem() { Text = title, Value = imagePath });
}
}
con.Close();
}
return new SelectList(list, "Value", "Text");
}
}
控制器:
public ViewResult ShowImages()
{
Image image = new Image();
return View(image);
}
VIEW(尝试同时显示所有图片):
@foreach (var image in Model.ImageList)
{
<img src="@Url.Content(image)" alt="image" id="image" style="width:200px;height:200px;" />
}
答案 0 :(得分:2)
我建议稍微重构一下你的代码。以下是一些简单的事情,但正确地分离了代码。以下内容将在页面上一次显示所有图像。
public class Image
{
public string Title {get; set;}
public string ImagePath {get; set;}
}
public class ImageRepository
{
public static IEnumerable<Image> GetImages(){
var list = new List<Image>();
string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var con = new SqlConnection(connection))
{
con.Open();
using (var command = new SqlCommand("SELECT * FROM myimage", con))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string title = reader[1] as string;
string imagePath = reader[2] as string;
list.Add(new Image() { Title = title, ImagePath = imagePath });
}
}
con.Close();
}
return list;
}
}
public ViewResult ShowImages()
{
var images = ImageRepository.GetImages();
return View(images);
}
@foreach (var image in Model)
{
<img src="@Url.Content(image.ImagePath)" alt="image.Title" style="width:200px;height:200px;" />
}
要将其放在下拉列表中,您可以将控制器代码修改为以下内容(您不希望在SQL代码中包含该SelectList)。
public ActionResult ShowImages()
{
var images = ImageRepository.GetImages();
var imageSelectList = new SelectList(images, "ImagePath", "Title");
return View(imageSelectList);
}
@Html.DropDownList("ImageList", Model);
我会从这里走得更远,并且也有适当的ViewModel。
public class ShowImagesViewModel{
public ShowImagesViewModel(IEnumerable<Image> images){
this.Images = images;
}
public IEnumerable<Image> Images {get; private set;}
public SelectList ImageSelectList {
get
{
return new SelectList(images, "ImagePath", "Title");
}
}
}
public ActionResult ShowImages()
{
var images = ImageRepository.GetImages();
var model = new ShowImagesViewModel(images);
return View(model);
}
@Html.DropDownList("ImageList", Model.ImageSelectList);
答案 1 :(得分:0)
你在寻找这样的东西:
@for (var i = 0; i < Model.ImageList.Count; i++)
{
<img src="@Url.Content(Model.ImageList[i].Value)" alt="image" id="'image' + @i.ToString()" style="width:200px;height:200px;" />
}