gpt4 book ai didi

c# - 使用字符串作为 Id 测试详细信息操作

转载 作者:行者123 更新时间:2023-11-30 22:53:29 25 4
gpt4 key购买 nike

the Index[the blank detail page] 1 page我正在尝试使用最小起订量为具有字符串类型作为键的详细方法编写测试。我创建了一个接口(interface)和存储库,该接口(interface)和存储库具有通过测试的方法 GetOne(string id)。我的问题是当我运行应用程序并导航到详细信息链接时,页面显示为空白。

我到处搜索,但找不到任何解决方案。

// The Repo
public class PostcodesRepository : IPostcodesRepository
{
private readonly sample6Context _context;

public PostcodesRepository(sample6Context context)
{
_context = context;
}

public Task<List<Postcode>> GetAll() =>
_context.Postcode.Include(p => p.DataZoneNavigation).AsNoTracking().OrderByDescending(a => a.Postcode1).ToListAsync();

public Task<Postcode> GetOne(string id) =>
_context.Postcode.Include(p => p.DataZoneNavigation).AsNoTracking().FirstOrDefaultAsync(m => m.Postcode1 == id);
}
}
// Model
public class Postcode
{
[key]
public string Postcode1 { get; set; }
public string DataZone { get; set; }

public virtual Data DataZoneNavigation { get; set; }
}
// The Interface
public interface IPostcodesRepository
{
Task<List<Postcode>> GetAll();
Task<Postcode> GetOne(string id);
}
// The Controller
public class PostcodesController : Controller
{
private readonly IPostcodesRepository _postcodesRepository;

public PostcodesController(IPostcodesRepository postcodesRepository)
{
_postcodesRepository = postcodesRepository;
}

// GET: Postcodes
public async Task<IActionResult> Index()
{
return View(await _postcodesRepository.GetAll());
}

// GET: Postcodes/Details/5
public async Task<IActionResult> Details(string id)
{
if (id == null)
{
return NotFound();
}

var postcode = await _postcodesRepository.GetOne(id);

if (postcode == null)
{
return NotFound();
}

return View(postcode);
}
}
// The Detail view
@model abdndata.Models.Postcode

@{
ViewData["Title"] = "Details";
}

<h1>Details</h1>

<div>
<h4>Postcode</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Postcode1)
</dt>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.DataZone)
</dt>

</dl>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
// Routing
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

我希望 detail 方法能够正常运行并显示信息。

最佳答案

在您的详细信息 View 标记中,您不显示数据。您仅显示显示名称。

<div>
<h4>Postcode</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Postcode1)
</dt>
<dd class="col-sm-2">
@Model.Postcode1
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.DataZone)
</dt>
<dd class="col-sm-2">
@Model.DataZone
</dd>
</dl>
</div>

所以问题是模型的数据在显示时被省略了

关于c# - 使用字符串作为 Id 测试详细信息操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com