MVC - 在我的视图中显示来自另一个模型的图片

时间:2017-05-04 15:00:05

标签: c# asp.net-mvc entity-framework linq

我目前正在开展一个个人项目,到目前为止一切工作都很顺利但是现在我无法展示我的照片,而且目前没有关于如何将它们放在一起的想法。

CarModel

public class Car
{
    public int Id { get; set; }

    public string Brand{ get; set; }

    public string Type { get; set; }

    [Range(1900,2017)]
    public int Buildyear { get; set; }

    public float Price { get; set; }

PictureModel

    public class Picture
{
    public int Id { get; set; }

    public int CarId{ get; set; }

    public string UploadedFile { get; set; }

    public DateTime UploadDate { get; set; }
}

CarSearchResultViewModel

public class CarSearchResultViewModel
{
    [Display(Name = "Brand ")]
    public string Brand { get; set; }

    [Display(Name = "Type ")]
    public string Type { get; set; }

    public int Buildyear { get; set; }

    public float Price { get; set; }

    public Brandstof Fuel { get; set; }

    public string Extra_Information { get; set; }

    public Transmission Transmission { get; set; }


    //Either of these 2 are not necessary
    public List<Picture> Pictures { get; set; }

    public string UploadedFile { get; set; }

}

CarController

List<CarSearchResultViewModel> result = new List<CarSearchResultViewModel>();
        if (ModelState.IsValid)
        {
            result = (from pd in db.Cars
                      join od in db.Pictures on pd.Id equals od.CarId into odPictures                          
                      where pd.Brand == autoZVM.Brand 
                      && pd.Fuel == autoZVM.Fuel
                      && pd.Type == autoZVM.Type
                      && pd.Buildyear > autoZVM.MinBuildyear && pd.Buildyear < autoZVM.MaxBuildyear 
                      && pd.Price > autoZVM.MinPrice && pd.Price < autoZVM.MaxPrice 
                      select new CarSearchResultViewModel
                      {
                          Brand= pd.Brand,
                          Type = pd.Type,
                          Fuel = pd.Fuel ,
                          Buildyear = pd.Buildyear,
                          Pictures = odPictures.ToList(),                           
                          Price= pd.Price,
                          Extra_Information = pd.Extra_Information

                      }).ToList();
        }
        return View("Result", result);

ResultView

@model IEnumerable<The_Scrubberij.Models.AutoZoekResultaatViewModel>

@{
    ViewBag.Title = "Result";
}

<h2>Result</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>


<table class="table">
    <tbody>
        @foreach (var item in Model)
        {
            <div class="panel panel-default">

                <div class="panel-heading">
                    <h3 class="panel-title">
                        @Html.DisplayFor(modelitem => item.Brand) - @Html.DisplayFor(modelItem => item.Type)

                    </h3>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-8">

                            @Html.DisplayFor(modelItem => item.Price) - @Html.DisplayFor(modelItem => item.Buildyear)
                        </div>
                        <div class="col-md-4">
                            @Html.DisplayFor(modelItem => item.Extra_Information)   -  <img src="@Url.Content(Model.UploadedFile)"></img>


                            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
                        </div>
                    </div>
                </div>
            </div>
        }
    </tbody>

    </table>
  

IEnumerable&#39;不包含&#39; UploadedFile&#39;的定义没有扩展方法&#39; UploadedFile&#39;接受类型&#39; IEnumerable&#39;的第一个参数。可以找到

这是与简单SQL查询的表关系 enter image description here

我不知道如何将这两个事物联系在一起提示

我的谢谢,Leroy

3 个答案:

答案 0 :(得分:1)

您可以简单地拥有一个<img>元素,并将源设置为文件的路径,假设存储的路径可以从客户端完全按原样访问。或者使用Url.Content而不是属性中的原始值。

<img src="@Url.Content(Model.UploadedFile)"></img>

编辑:哦,我坚持认为你没有汽车和照片之间的关系。看到汽车可以有多张图片,你需要CarSearchResultViewModel上的图片列表,你需要用连接返回的图片列表填充它。可能类似于:

        result = (from pd in db.Cars
                  join od in db.Pictures on pd.Id equals od.CarId
                  where ...
                  select new CarSearchResultViewModel
                  {
                      Brand= pd.Brand,
                      ...,
                      Pictures = od.ToList()
                  }).ToList();

我没有测试过这个,因为我不想设置一个完整的项目,而是喜欢它。

答案 1 :(得分:0)

你可以解决问题很简单,你使用<img> src属性

你有编辑控制器

result = (from pd in db.Cars
                  join od in db.Pictures on pd.Id equals od.CarId
                  where pd.Brand == autoZVM.Brand 
                  && pd.Fuel == autoZVM.Fuel
                  && pd.Type == autoZVM.Type
                  && pd.Buildyear > autoZVM.MinBuildyear && pd.Buildyear < autoZVM.MaxBuildyear 
                  && pd.Price > autoZVM.MinPrice && pd.Price < autoZVM.MaxPrice 
                  select new CarSearchResultViewModel
                  {
                      Brand= pd.Brand,
                      Type = pd.Type,
                      Fuel = pd.Fuel ,
                      Buildyear = pd.Buildyear ,                           
                      Price= pd.Price,
                      Extra_Information = pd.Extra_Information,
                      Pictures=Od.UploadedFile
                  }).ToList();

答案 2 :(得分:0)

在其他助手的帮助下,我终于得到了我的解决方案,因为我做了什么

在我的CarSearchResultViewmodel中,我添加了一个图片列表

    public class AutoZoekResultaatViewModel
{
    [Display(Name = "Brand ")]
    public string Brand { get; set; }
    ...
    public List<Picture> Pictures{ get; set; }
}

之后我在Alex Praven的帮助下编辑了我的LINQ查询

from pd in db.Cars
join od in db.Pictures on pd.Id equals od.CarId into odPictures
where pd.Brand == autoZVM.Brand
...

select new CarSearchResultviewModel
{
  Brand = pd.Brand,
  Pictures = odPictures.ToList(),
}).ToList();

和我的Result.cshtml

 @foreach (var Picture in item.Pictures)
 {
  <img src="/pictures/@Url.Content(foto.UploadedFile)"></img>
 }