. net Core中的级联下拉列表



我正试图使用下面链接的教程,但使其适应我的SQL DB的州,地区,学校表,我已经在适当的地方。我是新的。net Core MVC和不理解的错误,也不知道如何调试它。谢谢你的帮助。

.NET Core中的级联下拉列表

错误:Microsoft.Data.SqlClient.SqlException: '无效的对象名称'State'。'这个异常最初是在这个调用栈上抛出的:(外部代码)CascadingExample.Controllers.HomeController.Index() in HomeController.cs

[External Code]
using CascadingExample.Entities;
using CascadingExample.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace CascadingExample.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly MyDBContent _con;
public HomeController(ILogger<HomeController> logger, MyDBContent con)
{
_logger = logger;
_con = con;
}
public IActionResult Index()
{
ViewBag.StateList = _con.State.ToList();
return View();
}

public JsonResult GetDistrictByStateID(int statedID)
{
var data = _con.District.Where(x => x.StateID == statedID).ToList();
return Json(data);
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}

这个错误意味着名为" State "的表还没有创建或者你引用的数据库还没有创建

我检查了你教程中的dbcontext,它不会创建表"State"

public class MyDBContent:DbContext
{
private IConfigurationRoot _config;
public MyDBContent(IConfigurationRoot config, DbContextOptions options) : base(options)
{
_config = config;
}
public DbSet<Category> Category { get; set; }
public DbSet<SubCategory> SubCategory { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(_config["ConnectionStrings:DefaultConnection"]);//connection string get by appsetting.json
}
}

你需要添加这些代码:

public DbSet<State> State { get; set; }

最新更新