gpt4 book ai didi

c# - ASP NET MVC C# - .Model 是一个属性,但它像一个类型一样使用

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:01 25 4
gpt4 key购买 nike

我在访问要查看的模型的属性时遇到困难。

  1. Error: Mvc.Webviewpage<tmodel>.Model is a property but it used like a type .在这段代码中 @foreach (Model.ShoutboxObject record in Model)

  2. 我想访问 Model 的属性称为 ShoutboxId在我看来,但有一个错误。

  3. 谁能帮我解决这个问题。我才刚刚开始学习这个。

模型 View 中的代码:

public class ShoutboxMainViewModel
{
public dynamic DepartmentId { get; set; }

// Here is the property that I want to access
public IEnumerable<ShoutboxViewModel> ShoutboxObject { get; set; }

public class ShoutboxViewModel
{
// I want to access this to my view and put into input type hidden.
public string ShoutboxId { get; set; }

public dynamic ShoutboxTitle { get; set; }

我的代码 .cshtml查看:

@model IEnumerable<WMSPortal.Models.ShoutboxMainViewModel>

@foreach (Model.ShoutboxObject record in Model) { *//error here*

<text>
<input type="hidden" class="shoutbox-id" value="@record.ShoutboxId" />
</text>

最佳答案

您将需要 2 个循环(或使用 LINQ),因为您有一个 IEnumerable<WMSPortal.Models.ShoutboxMainViewModel>模型,和一个 IEnumerable<ShoutboxViewModel>该模型的属性。

@foreach (var viewmodel in Model) {
@foreach (var record in viewmodel.ShoutboxObject) {
<text>
<input type="hidden" class="shoutbox-id" value="@record.ShoutboxId" />
</text>
}
}

关于c# - ASP NET MVC C# - .Model 是一个属性,但它像一个类型一样使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com