昨天,我花了大量时间搜索并解决此问题,但没有提出解决方案。这是我用作参考的指南。
问题在于表单中的数据未到达我的控制器。我的目标是获取表格数据并将其传递给控制器/模型,以便我可以在整个代码中使用这些值并将其存储在数据库中。以下是我到目前为止所拥有的...
在我的Browse.cshtml中(查看)
@model Collect
<form asp-action="Collect" asp-controller="Collect" method="post">
<input type="hidden" asp-for="GameId" name="@game.id"/>
<button type="submit" class="dropdown-item btn btn-block">Default</button>
</form>
Run Code Online (Sandbox Code Playgroud)
在我的CollectController.cs(控制器)中
using System;
using GameLibrary.me.Models;
using Microsoft.AspNetCore.Mvc;
namespace GameLibrary.me.Controllers
{
public class CollectController : Controller
{
[HttpGet]
public IActionResult Collect()
{
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public IActionResult Collect(Collect model)
{
Console.WriteLine("**********\n"+model.GameId+"\n**********");
return Content($"Hello {model.GameId}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的Collect.cs(模型)中
namespace GameLibrary.me.Models
{
public class Collect
{
public int GameId { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是我的IDE告诉我的...
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting …Run Code Online (Sandbox Code Playgroud) 抱歉,这可能是一个非常业余的问题,但我很难理解如何正确使用起订量。总的来说,我对单元测试还很陌生,但我想我已经开始掌握它的窍门了。
所以这是我的问题...我下面有这段代码,它TestServer在 Visual Studio 中使用我用于单元测试的代码...我试图进行模拟,IGamesByPublisher以便我的测试不依赖于存储库中的数据(或者嘲笑会更好吗GamesByPublisher?...或者我需要两者都做吗?)
public static TestServerWithRepositoryService => new TestServer(services =>
{
services.AddScoped<IGamesByPublisher, GamesByPublisher();
}).AddAuthorization("fake.account", null);
[Fact] // 200 - Response, Happy Path
public async Task GamesByPublisher_GamesByPublisherLookup_ValidRequestData_Produces200()
{
// Arrange
var server = ServerWithRepositoryService;
// Act
var response = await server.GetAsync(Uri);
// Assert
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
Run Code Online (Sandbox Code Playgroud)
这里是IGamesByPublisher
public interface IGamesByPublisher interface.
{
Task<IEnumerable<Publisher>> Execute(GamesByPublisherQueryOptions options);
}
}
Run Code Online (Sandbox Code Playgroud)
我试过
public static TestServerWithRepositoryService => new TestServer(services =>
{
services.AddScoped<Mock<IGamesByPublisher>, Mock<GamesByPublisher>>();
}).AddAuthorization("fake.account", …Run Code Online (Sandbox Code Playgroud)