[英]How to display count in view from a different controller
I changed the Person controller and Home view.我更改了 Person 控制器和 Home 视图。
I have 209 people in the database.我的数据库中有 209 个人。 And that's how it shows me in the Person view.
这就是它在 Person 视图中显示的方式。
When I interrupt the code count shows me 0.当我中断时,代码计数显示为 0。
View Home: I also made changes here.查看主页:我也在这里进行了更改。
@model Legolandia.Models.Store
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<div class="row">
@section Scripts {
<script>
// document.ready is fired once the page has loaded in the browser
$(document).ready(function () {
// set the url to get the data from the PersonController
var url = '@Url.Action("GetPersonCount", "Person")';
// use jQuery.getJSON to get our data from the controller
$.getJSON(url, function(data) {
// update the element with the count from the controller
$('#personCount').text(data.count);
});
});
</script>
}
<div id="orange" class="col-md-4">
<a class="change-a" href="/Person/Index">
<div class="change-title">
Person in system: <span id="personCount"></span>
</div>
Controller Person:控制人:
I also made changes here.我也在这里进行了更改。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Legolandia.Models;
namespace Legolandia.Controllers
{
public class PersonController : Controller
{
private LegolandEntities db = new LegolandEntities();
// GET: Person
public ActionResult Index()
{
var person = db.Person.Include(o => o.xxx);
ViewBag.PersonCount = db.Person.Count();
return View(person.ToList());
}
public ActionResult GetPersonCount()
{
var count = db.Person.Count(); // get count from database
var data = new { count }; // anonymous type
return Json(data); // return serialized Json data
}
Since you need to get data from PersonController
and display it in a view of HomeController
, I'd suggest using Ajax.由于您需要从
PersonController
获取数据并将其显示在HomeController
的视图中,因此我建议使用 Ajax。
Create a new method in PersonController
to return the data you need.在
PersonController
创建一个新方法来返回您需要的数据。 This method will return Json data which you can use in the view.此方法将返回您可以在视图中使用的 Json 数据。
public ActionResult GetPersonCount()
{
var count = db.Person.Count(); // get count from database
var data = new { count }; // anonymous type
return Json(data, JsonRequestBehavior.AllowGet); // return serialized Json data
}
Note: If you're using .Net Core you don't need the JsonRequestBehavior.AllowGet
in the Json result.注意:如果您使用 .Net Core,则不需要 Json 结果中的
JsonRequestBehavior.AllowGet
。
Now, in Home/Index.cshml
you will need to add some JavaScript.现在,在
Home/Index.cshml
您需要添加一些 JavaScript。 I'll actually use jQuery since it makes this task a little easier.我实际上会使用 jQuery,因为它使这个任务更容易一些。 Make sure you include jQuery on the page (MVC comes with jQuery included these days).
确保在页面上包含 jQuery(现在 MVC 附带了 jQuery)。
First, in Home/index.cshtml
create a <span>
element which will hold the value for PersonCount
:首先,在
Home/index.cshtml
创建一个<span>
元素,它将保存PersonCount
的值:
<div class="change-title">
Person in system: <span id="personCount"></span>
</div>
Now, near the bottom of Home/index.cshtml
add some script tags:现在,在
Home/index.cshtml
底部附近添加一些脚本标签:
@section Scripts {
<script>
// document.ready is fired once the page has loaded in the browser
$(document).ready(function () {
// set the url to get the data from the PersonController
var url = '@Url.Action("GetPersonCount", "Person")';
// use jQuery.getJSON to get our data from the controller
$.getJSON(url, function(data) {
// update the element with the count from the controller
$('#personCount').text(data.count);
});
});
</script>
}
Finally, in _Layout.cshtml
make sure you put @RenderSection("Scripts", required: false)
near the bottom of the page (below @RenderBody()
).最后,在
_Layout.cshtml
确保将@RenderSection("Scripts", required: false)
放在页面底部附近( @RenderBody()
下方)。
Here I have used $.getJSON
.在这里我使用了
$.getJSON
。 It performs an ajax request to Person/GetPersonCount
.它向
Person/GetPersonCount
执行 ajax 请求。 Then we update the <span>
with the person count from the GetPersonCount
in PersonController
.然后我们用
PersonController
GetPersonCount
的PersonController
更新<span>
。
Here's the result:结果如下:
这种方法对我有用:
ViewBag.PatientCount = (from x in Hasta.TblPatient select x).Count();
In your controller, save count in ViewData
:在您的控制器中,将计数保存在
ViewData
:
{
ViewData["Users"] = _userManager.Users.Count();
return View();
}
In your view, retrieve the value:在您看来,检索值:
@Html.ViewData["Users"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.