这是我的动作方法:
[HttpGet]
[Route("{userId}")]
public IActionResult Show(int userId)
{
List<Dictionary<string, object>> User = DbConnector.Query($"SELECT * FROM users WHERE id = {userId}");
ViewBag.userId = 1;
return View();
}
这是我的index.cshtml
文件:
<h1>here is one user:</h1>
<h3>@ViewBag.userId</h3>
我想我在控制器上做错了什么,但我不确定是什么。
如下所示
定义User
数据模型:
public class User
{
[Display(Name = "User Id")]
public int UserId { get; set; }
// Another properties ...
}
在控制器中:
[HttpGet]
[Route("{userId}")]
public IActionResult Show(int userId)
{
User user = /* obtain the user record from the db */
// Pass the user to the strongly typed view.
return View(user);
}
在show.cshtml
:
@model Models.User
<!DOCTYPE html>
<html>
<body>
<div class="column">
@Html.DisplayForModel()
</div>
</body>
</html>