[英]ASP.net core View does not return value to controller
What I am trying to achieve is 2 things:我想要实现的是两件事:
So in Summary my View never sends my Controller the ID.所以总而言之,我的视图永远不会向我的 Controller 发送 ID。 and I also get this other error when I try to access my database.
当我尝试访问我的数据库时,我也遇到了这个错误。 (this error happens on startup without me even trying to access that part of the functions)
(这个错误发生在启动时,我什至没有尝试访问那部分功能)
My view is something like that.我的观点是这样的。 I'll only post the affected: so what happens here.
我只会发布受影响的:所以这里会发生什么。 my list loads exactly as expected and shows everything I need.
我的列表完全按预期加载并显示了我需要的一切。 after I click on Details and Access my controller it will not give me a value.
在我点击详细信息并访问我的 controller 后,它不会给我一个值。 I will explain later in the other block of code
稍后我将在其他代码块中解释
<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>
So what is happening here once I start up my project I will get that error:所以一旦我启动我的项目,这里发生了什么我会得到这个错误:
InvalidOperationException: Unable to resolve service for type 'MoviesDB.Models.ApplicationDbContext' while attempting to activate 'MoviesDB.Controllers.HomeController'.
InvalidOperationException:尝试激活“MoviesDB.Controllers.HomeController”时无法解析“MoviesDB.Models.ApplicationDbContext”类型的服务。
I don't know why this is happening, I will post my services on the next block just in case there is something I am missing in there.我不知道为什么会这样,我会在下一个区块发布我的服务,以防万一我在那里遗漏了什么。 well now if I commented out all the database things and just have inside
FindMovie
action现在好了,如果我注释掉所有数据库的东西并且只有在
FindMovie
动作里面
Console.WriteLine("I am in Find" + imdbID);
I will only get "I am in Find" but I will not have an id next to it.我只会得到“我在查找中”,但旁边不会有 id。 so as it seems I am not sending the 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}");
}
and lastly here are my services:最后是我的服务:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
I have found what the problem was.我发现了问题所在。 When creating a new project with visual studio.
使用 Visual Studio 创建新项目时。 if you choose to create Authentication, it will create a dbcontext for you inside a folder called Data.
如果您选择创建身份验证,它将在名为 Data 的文件夹中为您创建一个 dbcontext。 it so happened this file to be called ApplicationDbContext as well.
碰巧这个文件也被称为 ApplicationDbContext 。 out of coincidence I named it the same.
出于巧合,我将其命名为相同。
what I did to resolve the error was to rename my own ApplicationDbContext to something new and add it in services and it worked perfectly.我为解决该错误所做的是将我自己的 ApplicationDbContext 重命名为新名称并将其添加到服务中,它运行良好。 it was a structure error I guess.
我猜这是一个结构错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.