【发布时间】:2021-03-23 14:32:48
【问题描述】:
我试图通过使用视图模型在视图上显示变量 testing (= 55) 的值。有人可以帮忙吗?
类(Calculations.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace tool3.Models
{
public class Calculations
{
public int Amount { get; set; }
}
}
控制器(CalculationsController.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using tool3.Models;
namespace tool3.Controllers
{
public class CalculationsController : Controller
{
public IActionResult Index()
{
var testing = new Calculations() { Amount = 55 };
return View(testing);
}
}
}
视图模型(EFViewModel.cs):
using tool3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace tool3.ViewModel
{
public class EFViewModel
{
public Calculations Calculations { get; set; }
}
}
索引视图(Index.cshtml),我只想在这里显示数字“55”:
@model tool3.ViewModel.EFViewModel
@Model.Calculations.Amount
我得到错误:
System.NullReferenceException: '对象引用未设置为对象的实例。' tool3.ViewModel.EFViewModel.Calculations.get 返回 null。
提前致谢
【问题讨论】:
-
var testing = new EFViewModel() { Calculations = new Calculations() { Amount = 55 }};
标签: c# asp.net asp.net-core model-view-controller viewmodel