1

Currently I am dabbling in ASP.NET Core 6.0 and got stuck on trying to populate a view model.

I have the following linq(?) query (is it even called that?)

var fotoGeneratedNames = _context.Foto 
                                 .Where(x => fotoIds.Contains(x.Id))
                                 .Select(x => x.generatedName)
                                 .ToList();

Controller code

// Diese Methode gibt die notwendigen Daten für eine spezifische Gallerie zurück 
public IActionResult Gallerie(int id) 
{ 
    // instantiert neue Var mit dem Expose Viewmodel Gallerie
    // var viewModelExposeGallerie = new ViewModelExposeGallerie(); 

    // nimm gallerieId, such alle FotoIds aus associationtable raus
    var fotoIds = (from a in _context.GallerieFoto 
                   where a.GallerieId == id select a.FotoId).ToList();

    // Nimm alle elemente in der tabelle Foto mit den Elementen in fotoIds und mach eine Liste mit den generiertenNamen der Fotos
    var fotoGeneratedNames = _context.Foto
                                     .Where(x => fotoIds.Contains(x.Id))
                                     .Select(x => x.generatedName)
                                     .ToList();

    return View(fotoGeneratedNames);
}  

This is my view model

namespace abc.Models
{
    public class ViewModelExposeGallerie
    {
        public string FotoName { get; set; }    
    }
}

Now - what I tried was a foreach akin to

foreach var x in fotoGeneratedNames 
    ViewModelExposeGallerie.FotoName = fotoGeneratedNames 

but this only replaced the value and did not give me a list, that I could use in the view.

Thankful for any hints, have a good one!

2
  • Could you show us want you want to do in view? Commented May 22, 2022 at 1:01
  • pretty much just something like ``` @model ienumerable abc.models.viewmodel @foreach { <img src @item > </img> } ``` Commented May 22, 2022 at 1:47

1 Answer 1

3

In your controller:

public IActionResult Gallerie(int id) { 

    ...
             
    var fotoGeneratedNames = _context.Foto
        .Where(x => fotoIds.Contains(x.Id))
        .Select(x => x.generatedName)
        .ToList();

    return View(fotoGeneratedNames);
}  

You are returning a List<string> but not List<ViewModelExposeGalleri> type

Your view should be as below:

@model IEnumerable<string>

@foreach (var name in Model)
{
    <div>@name</div>
}

Unsure what you want to render for the HTML element, so I illustrate the demo with <div> element.


If you return List<ViewModelExposeGalleri> from the Controller:

public IActionResult Gallerie(int id) { 

    ...
             
    var fotoGeneratedList = _context.Foto
        .Where(x => fotoIds.Contains(x.Id))
        .Select(x => new ViewModelExposeGallerie
        {
            FotoName = x.generatedName
        })
        .ToList();

    return View(fotoGeneratedList);
} 
@model IEnumerable<abc.models.ViewModelExposeGalleri>

@foreach (var item in Model)
{
    <div>@item.FotoName</div>
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.