I changed the Person controller and Home view.
I have 209 people in the database. And that's how it shows me in the Person view.
When I interrupt the code count shows me 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.
Create a new method in PersonController
to return the data you need. This method will return Json data which you can use in the view.
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.
Now, in Home/Index.cshml
you will need to add some JavaScript. I'll actually use jQuery since it makes this task a little easier. Make sure you include jQuery on the page (MVC comes with jQuery included these days).
First, in Home/index.cshtml
create a <span>
element which will hold the value for 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:
@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()
).
Here I have used $.getJSON
. It performs an ajax request to Person/GetPersonCount
. Then we update the <span>
with the person count from the GetPersonCount
in PersonController
.
Here's the result:
这种方法对我有用:
ViewBag.PatientCount = (from x in Hasta.TblPatient select x).Count();
In your controller, save count in ViewData
:
{
ViewData["Users"] = _userManager.Users.Count();
return View();
}
In your view, retrieve the value:
@Html.ViewData["Users"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.