HTML与模型一起使用C#的错误

发布于 2025-02-13 07:36:41 字数 2548 浏览 0 评论 0原文

我创建了一个存储库,现在我想使用foreach将多个项目添加到页面上,但它给了我一个错误。我的目的是:当页面中没有项目时,它将显示一条消息,但是如果页面的项目将显示不同的消息和项目。代码如下:

控制器:

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Treino1.Models;

namespace Treino1.Controllers
{
    public class DespesasController : Controller
    {
        public IActionResult Index()
        {
            List<Despesa> despesas = RepositorioDespesas.Despesas;
            return View(despesas);
        }


        [HttpGet]
        public IActionResult NovaDespesa()
        {
            return View();
        }

        [HttpPost]
        public IActionResult NovaDespesa(Despesa despesa)
        {
            if (ModelState.IsValid)
            {
                RepositorioDespesas.AddDespesa(despesa);
                return View("DespesaConfirmada", despesa);
            }
                return View();
        }
    }
}

存储库

using System.Collections.Generic;

namespace Treino1.Models
{
    public static class RepositorioDespesas
    {
        private static List<Despesa> despesas = new List<Despesa>();

        public static List<Despesa> Despesas
        {
            get { return despesas; }
        }

        public static void AddDespesa (Despesa newDespesa)
        {
            despesas.Add (newDespesa);
        }



    }
}



@{
    ViewData["Title"] = "Despesa Confirmada";
}

@model List<Treino1.Models.Despesa>


@if (Model.Count== 0)
{
    <h1>Sem Despesas...</h1>
}
else
{
    <h1>Despesas!</h1>
    @foreach (Treino1.Models.Despesa d in Model)
    {
        <div class="card bg-secondary border-dark">
            <div class="card-body">
                <b>Nome da Despesa: </b> @d.NomeDespesa
                <b>Quantidade: </b> @d.Quantidade
                <b>Valor: </b> @d.Valor
                <b>Categoria: </b> @d.Categoria
                <b>Pago? </b>
                @if (d.Pago)
            {
             @:Sim
         }else
            {
              @:Não
            }

        </div>
    </div>
    
}

}

<div>
    <a asp-action="NovaDespesa">Nova Despesa</a>
</div>

我认为错误是关于模型的。COUNT== 0,但我不知道该如何解决。

错误是:

“在此处输入图像描述”

I created a repository and now i want to use a foreach to add multiple items to the page but its giving me a error. My objective is: when there is no items in the page it will show a message but if the page have items will show a different message and the items. The code is the following:

Controller:

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Treino1.Models;

namespace Treino1.Controllers
{
    public class DespesasController : Controller
    {
        public IActionResult Index()
        {
            List<Despesa> despesas = RepositorioDespesas.Despesas;
            return View(despesas);
        }


        [HttpGet]
        public IActionResult NovaDespesa()
        {
            return View();
        }

        [HttpPost]
        public IActionResult NovaDespesa(Despesa despesa)
        {
            if (ModelState.IsValid)
            {
                RepositorioDespesas.AddDespesa(despesa);
                return View("DespesaConfirmada", despesa);
            }
                return View();
        }
    }
}

Repository

using System.Collections.Generic;

namespace Treino1.Models
{
    public static class RepositorioDespesas
    {
        private static List<Despesa> despesas = new List<Despesa>();

        public static List<Despesa> Despesas
        {
            get { return despesas; }
        }

        public static void AddDespesa (Despesa newDespesa)
        {
            despesas.Add (newDespesa);
        }



    }
}



@{
    ViewData["Title"] = "Despesa Confirmada";
}

@model List<Treino1.Models.Despesa>


@if (Model.Count== 0)
{
    <h1>Sem Despesas...</h1>
}
else
{
    <h1>Despesas!</h1>
    @foreach (Treino1.Models.Despesa d in Model)
    {
        <div class="card bg-secondary border-dark">
            <div class="card-body">
                <b>Nome da Despesa: </b> @d.NomeDespesa
                <b>Quantidade: </b> @d.Quantidade
                <b>Valor: </b> @d.Valor
                <b>Categoria: </b> @d.Categoria
                <b>Pago? </b>
                @if (d.Pago)
            {
             @:Sim
         }else
            {
              @:Não
            }

        </div>
    </div>
    
}

}

<div>
    <a asp-action="NovaDespesa">Nova Despesa</a>
</div>

I think the error is something about the Model.Count==0 but i dont know how to solve.

The error is this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2025-02-20 07:36:41

despesaconfirmada强烈键入的视图中的数据模型定义为@model list&lt; treino1.models.models.despesa&gt; Despesa despesa)操作方法单一treino1.models.despesa对象传递。尝试使用以下方式使用:

public IActionResult NovaDespesa(Despesa despesa)
{
    if (ModelState.IsValid)
    {
        RepositorioDespesas.AddDespesa(despesa);

        // Obtain corresponded records from the repository and pass to the view. 
        return View("DespesaConfirmada", RepositorioDespesas.Despesas());
    }
    return View();
}

The data model in the DespesaConfirmada strongly typed view is defined as @model List<Treino1.Models.Despesa>, but when it rendered from the public IActionResult NovaDespesa(Despesa despesa) action method the single Treino1.Models.Despesa object is passed. Try to use like below:

public IActionResult NovaDespesa(Despesa despesa)
{
    if (ModelState.IsValid)
    {
        RepositorioDespesas.AddDespesa(despesa);

        // Obtain corresponded records from the repository and pass to the view. 
        return View("DespesaConfirmada", RepositorioDespesas.Despesas());
    }
    return View();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文