我如何获得用户id从我的数据库,并使用ASP显示它.. NET Core MVC



这是我的动作方法:

[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>

最新更新