简体   繁体   English

ASP.net 核心视图不返回值到 controller

[英]ASP.net core View does not return value to controller

What I am trying to achieve is 2 things:我想要实现的是两件事:

  1. send a value I get from an external source (An API) inside my controller for processing.发送我从 controller 内部的外部源(API)获得的值进行处理。 "to check if that ID exists in my database. if not add it" but my issue is that I always get null when I do so. “检查该ID是否存在于我的数据库中。如果不添加它”但我的问题是我这样做时总是得到null。 the ID is always empty even though I can view it inside the view.即使我可以在视图中查看它,该 ID 也始终为空。 but even with empty id, I should be able to search my database.但即使 id 为空,我也应该能够搜索我的数据库。
  2. that's where my second problem comes InvalidOperationException: Unable to resolve service for type 'MoviesDB.Models.ApplicationDbContext' while attempting to activate 'MoviesDB.Controllers.HomeController'.这就是我的第二个问题出现的地方 InvalidOperationException:尝试激活“MoviesDB.Controllers.HomeController”时无法解析“MoviesDB.Models.ApplicationDbContext”类型的服务。

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.

相关问题 HTTP Post不返回ASP.NET MVC Core 2中的视图 - HTTP Post does not return the view in ASP.NET MVC Core 2 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC ASP.net MVC控制器将不会返回视图 - ASP.net MVC Controller will not return view 使用ASP.NET Core API返回视图 - Return a view with ASP.NET Core API ASP.NET 内核 controller 返回“成功”框,而不是查看并停留在同一页面上 - ASP.NET Core controller return “success” box instead of view and stay on the same page 从 controller 返回 viewmodel 作为 model 以查看和获取 angularjs 并显示在 Asp.Net Core 3.1 的选项标签中? - return viewmodel as a model from controller to view and geting with angularjs and show in option tag in Asp.Net Core 3.1? 为什么脚手架ASP.NET Core Controller返回NoContent()而不返回OKSuccess()? - Why Does Scaffolded ASP.NET Core Controller return NoContent() and not OKSuccess()? ASP.NET Core MVC - 数据没有从视图传递到 controller 在 post 请求中 - ASP.NET Core MVC - Data does not pass from the view to the controller in a post request ASP.Net Core 3.1 - 从 Controller 将值传递给部分视图模式? - ASP.Net Core 3.1 - passing value to partial view Modal From Controller? 如何将任何选定的选项值从视图传递到 controller ASP.NET Core - How to pass any selected option value from a view to a controller ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM