ArgumentException:调用“DbSet.Find”时位置 0 处的键值类型为“string”,与 int 的属性类型不匹配;
当我尝试编辑我的帖子时,会引发此错误。
这是我的代码:
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
// issue
var news = await _context.News.FindAsync(id);
if (news == null)
{
return NotFound();
}
return View(news);
}
调试器将代码停止在
var news = await _context.News.FindAsync(id);
我的模型的代码
public int id { get; set; }
[Required(ErrorMessage = "Enter your name.")]
public string Author { get; set; }
[Required(ErrorMessage = "Enter the title.")]
public string Title { get; set; }
[Required(ErrorMessage = "Enter the issued date.")]
[DataType(DataType.Date)]
public DateTime IssueDate { get; set; }
[Required(ErrorMessage = "Enter a message.")]
[DataType(DataType.MultilineText)]
public string Body { get; set; }
处,您知道如何解决此问题吗?
This error is thrown when I try to edit my post.
Here is my code:
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
// issue
var news = await _context.News.FindAsync(id);
if (news == null)
{
return NotFound();
}
return View(news);
}
The debugger stops the code at
var news = await _context.News.FindAsync(id);
Code for my model is
public int id { get; set; }
[Required(ErrorMessage = "Enter your name.")]
public string Author { get; set; }
[Required(ErrorMessage = "Enter the title.")]
public string Title { get; set; }
[Required(ErrorMessage = "Enter the issued date.")]
[DataType(DataType.Date)]
public DateTime IssueDate { get; set; }
[Required(ErrorMessage = "Enter a message.")]
[DataType(DataType.MultilineText)]
public string Body { get; set; }
Any idea on how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据文档:
因此,如果您的情况中的主键具有
int
类型,则FindAsync()
参数应为相同的int
类型。According to the documentation:
Therefore, if the primary key in your case has
int
type, then theFindAsync()
parameter should be the same typeint
.最可靠的方法是
,但也许改变一个动作更好?
The most reliable way would be
but maybe it is better to change an action?