HTML与模型一起使用C#的错误
我创建了一个存储库,现在我想使用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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
despesaconfirmada
强烈键入的视图中的数据模型定义为@model list&lt; treino1.models.models.despesa&gt;
Despesa despesa)操作方法单一treino1.models.despesa
对象传递。尝试使用以下方式使用:The data model in the
DespesaConfirmada
strongly typed view is defined as@model List<Treino1.Models.Despesa>
, but when it rendered from thepublic IActionResult NovaDespesa(Despesa despesa)
action method the singleTreino1.Models.Despesa
object is passed. Try to use like below: