简体   繁体   English

ASP.NET Core 6.0 MVC - 从列表中填充视图模型

[英]ASP.NET Core 6.0 MVC - Populating a viewmodel from list

Currently I am dabbling in ASP.NET Core 6.0 and got stuck on trying to populate a view model.目前,我正在涉足 ASP.NET Core 6.0,但一直在尝试填充视图模型。

I have the following linq(?) query (is it even called that?)我有以下 linq(?) 查询(它甚至被称为?)

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!感谢您的任何提示,祝您好运!

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您正在返回List<string>但不是List<ViewModelExposeGalleri>类型

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.不确定要为 HTML 元素呈现什么,所以我用<div>元素来说明演示。


If you return List<ViewModelExposeGalleri> from the Controller:如果您从控制器返回List<ViewModelExposeGalleri>

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>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择列表未从ViewModel ASP.NET填充 - Select List not populating from ViewModel ASP.NET Asp.net MVC 核心,从 PopupView 发布以前的 ViewModel - Asp.net MVC core, posting previous ViewModel from PopupView 在 ASP.NET Core MVC 中从 ViewModel 更新 ICollection - Updating ICollection from ViewModel in ASP.NET Core MVC Asp.Net MVC Razor下拉菜单-从ViewModel访问列表 - Asp.Net MVC Razor Dropdown - Accessing list from ViewModel ASP.NET Core 6.0 MVC - 使用日期范围对字符串进行排序 - ASP.NET Core 6.0 MVC - Sort a string with range of date 在 ASP.NET Core 6.0 MVC 中设置身份验证令牌到期 - Set authentication token expiry in ASP.NET Core 6.0 MVC ASP.NET (6.0) Core MVC Login and Registration using Identity - ASP.NET (6.0) Core MVC Login and Registration using Identity 将 blob 从一个 Azure 容器复制到另一个使用 ASP.NET Core 6.0 MVC 和 C# - Copy blobs from one Azure container to another using ASP.NET Core 6.0 MVC with C# ASP.Net Core MVC - 复杂的 ViewModel 没有将属性与对象列表绑定 - ASP.Net Core MVC - Complex ViewModel not binding property with list of objects 如何使用 ASP.NET Core 3.1 MVC 中的 ViewModel 与 JavaScript 和 C# 动态添加到列表 - How to dynamically add to list with ViewModel in ASP.NET Core 3.1 MVC with JavaScript and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM