【发布时间】:2020-12-02 09:56:55
【问题描述】:
我正在尝试在 ASP.NET 中获取食物列表。我创建了一个模型 Menu 和一个模型 listMenu 来获取 Menucontroller 中的列表。我有以下代码,我得到这个 error:InvalidCastException: Unable to cast object of type 'System.Double' to type 'System.Single'。
我不知道问题是否出在数据库端,是否不是从表中读取
菜单控制器:
public async Task<IActionResult> Index(string searchString)
{
var foods = from m in _context.Menu
select m;
if (!string.IsNullOrEmpty(searchString))
{
foods = foods.Where(s => s.Name.Contains(searchString));
}
var foodlist = new ListMenu
{
Foods = await foods.ToListAsync()
};
return View(foodlist);
}
菜单模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Resto.Models
{
public class Menu
{
[Key]
public string Name { get; set; }
public float Price { get; set; }
}
}
ListMenu 模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Resto.Models
{
public class ListMenu
{
public List<Menu> Foods { get; set; }
public string SearchString { get; set; }
}
}
【问题讨论】:
-
Razor 页面标记和堆栈跟踪会很好。
标签: asp.net-core asp.net-core-mvc