[英]ASP.net core View does not return value to controller
我想要实现的是两件事:
所以总而言之,我的视图永远不会向我的 Controller 发送 ID。 当我尝试访问我的数据库时,我也遇到了这个错误。 (这个错误发生在启动时,我什至没有尝试访问那部分功能)
我的观点是这样的。 我只会发布受影响的:所以这里会发生什么。 我的列表完全按预期加载并显示了我需要的一切。 在我点击详细信息并访问我的 controller 后,它不会给我一个值。 稍后我将在其他代码块中解释
<tbody>
@foreach (var item in Model.Search)
{
@item.Runtime
// each table row is assigned with the imdbID returned from the API. if the user clicks one of the rows
//then the system will 1st check the database if this movie exist in the database. if not then it will be added and then desplayed
//if the movie exist then it will be automatically desplayed to the user without making another API call.
<tr >
<td><img src="@item.Poster" class="img-thumbnail" alt="Responsive image" width="200"> </td>
<td>@item.Title</td>
<td>@item.Year @item.imdbID</td>
<td> <a asp-controller="Home" asp-action="FindMovie" asp-for="@item.imdbID">Details </a> </td>
</tr>
}
</tbody>
所以一旦我启动我的项目,这里发生了什么我会得到这个错误:
InvalidOperationException:尝试激活“MoviesDB.Controllers.HomeController”时无法解析“MoviesDB.Models.ApplicationDbContext”类型的服务。
我不知道为什么会这样,我会在下一个区块发布我的服务,以防万一我在那里遗漏了什么。 现在好了,如果我注释掉所有数据库的东西并且只有在FindMovie
动作里面
Console.WriteLine("I am in Find" + imdbID);
我只会得到“我在查找中”,但旁边不会有 id。 所以看起来我没有发送身份证
private readonly ApplicationDbContext _db;
[BindProperty]
public Movies movies { get; set; }
public HomeController(ApplicationDbContext db)
{
_db = db;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult FindMovie()
{
return View();
}
[HttpPost]
public IActionResult FindMovie(string imdbID)
{
Console.WriteLine("I am in Find" + imdbID);
var item = _db.Movies.Where(s => s.imdbID.Equals(imdbID)).FirstOrDefault();
if (item != null)
{
return NotFound();
}
return Content("Id is: {imdbID}");
}
最后是我的服务:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
我发现了问题所在。 使用 Visual Studio 创建新项目时。 如果您选择创建身份验证,它将在名为 Data 的文件夹中为您创建一个 dbcontext。 碰巧这个文件也被称为 ApplicationDbContext 。 出于巧合,我将其命名为相同。
我为解决该错误所做的是将我自己的 ApplicationDbContext 重命名为新名称并将其添加到服务中,它运行良好。 我猜这是一个结构错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.