在这里,我打算在Index.cshtml中显示userIdCount。但是这个计数我会得到不同的动作结果,称为GetUser。但是,当调用getuser操作时,它将调用部分视图并将结果绑定到索引页。但我也想在索引视图上显示用户ID的数量。我尝试使用TempData,但无法正常工作
public ActionResult GetUser(string UserName, int? Locality, string ReportType)
{
UserManager usersManager = new UserManager();
List<ReportsInfo> reportsList = usersManager.GetRequestReport(UserName, Locality, "usp_GetUsers");
TempData["UserIdCount"] = reportsList.Select(x => x.UserId).Distinct().Count();
return PartialView("_ReportList", reportsList);
}
在索引视图中,我在下面使用了类似的方法。
<br /><br />
@if (TempData["UserIdCount"] != null)
{
var test = TempData["UserIdCount"];
<p>@test</p>
}
答案 0 :(得分:0)
您始终可以使用ViewBag。
public ActionResult GetUser(string UserName, int? Locality, string ReportType)
{
UserManager usersManager = new UserManager();
List<ReportsInfo> reportsList = usersManager.GetRequestReport(UserName, Locality,
"usp_GetUsers");
ViewBag.UserIdCount = reportsList.Select(x => x.UserId).Distinct().Count();
return PartialView("_ReportList", reportsList);
}
然后在您看来:
<p>@ViewContext.Controller.ViewBag.UserIdCount</p>
答案 1 :(得分:0)
您可以使用jquery或javascript来做到这一点。
public ActionResult GetUser(string UserName, int? Locality, string ReportType)
{
UserManager usersManager = new UserManager();
List<ReportsInfo> reportsList = usersManager.GetRequestReport(UserName, Locality,
"usp_GetUsers");
TempData["UserIdCount"] = reportsList.Select(x => x.UserId).Distinct().Count();
return PartialView("_ReportList", reportsList);
}
将索引页部分设置为这样
<p id="userIdCount"></p>
在GetUserPartialView中添加此脚本
<script type="text/javascript">
$(document).ready(function(){
$("#userIdCount").text('@TempData["UserIdCount"]');
});
</script>
记住,以在该脚本之前的视图中添加jQuery文件