Reputation: 117
I have a project in .Net core that generates ID Card. Everything works fine when I print ID Card for one students but I'm having problem printing multiple ID Cards.
I have get method in my controller as follows:
[HttpGet]
[AllowAnonymous]
public IActionResult BulkCard()
{
//values = ViewBag.Cards;
var values = (object[])TempData["students"]; //This retrieves all students that has been selected to print their ID Card
ViewBag.Cards = values.ToArray();
var designtemplate = _context.Settings.First();
if (designtemplate.CardTemplate == 1)
{
ViewBag.Template = "1";
}
else if (designtemplate.CardTemplate == 2)
{
ViewBag.Template = "2";
ViewBag.TemplateView = "This is template 2";
}
else if (designtemplate.CardTemplate == 3)
{
ViewBag.Template = "3";
ViewBag.TemplateView = "This is template 3";
}
else
{
return RedirectToAction("CardSettings", "Settings");
}
var model = new ModelClasses();
foreach (var item in values)
{
model.Students = _context.Students.ToList();
model.Student = _context.Students.Include(c => c.Department).Include(c => c.Department.Faculty).Include(c => c.ProgramType).Include(c => c.ClassLevels).Include(c => c.Session).ToList().FirstOrDefault(c => c.Id == Convert.ToInt32(item));
ViewBag.Students = _context.Students.Include(c => c.Department).Include(c => c.Department.Faculty).Include(c => c.ProgramType).Include(c => c.ClassLevels).Include(c => c.Session).ToList().Where(c => c.Id == Convert.ToInt32(item)).ToList();
var surname = model.Student.Surname;
var firstname = model.Student.FirstName;
var middleName = model.Student.MiddleName[0];
var fullname = surname + " " + firstname + " " + middleName + ".";
ViewBag.FullName = fullname;
ViewBag.MatricNumber = model.Student.MatricNumber;
ViewBag.Department = model.Student.Department;
ViewBag.Faculty = model.Student.Faculty;
ViewBag.Passport = "/Uploads" + "/Passport/" + model.Student.Passport;
ViewBag.Signature = "/Uploads" + "/Signature/" + model.Student.Signature;
var qrdata = model.Student.QRCodeData.ToString().Replace("/", "");
using (MemoryStream ms = new MemoryStream())
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrdata, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
bitMap.Save(ms, ImageFormat.Png);
ViewBag.QRCodeImage = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
}
}
}
return View(model);
}
And in my view is as follow:
@foreach (var item in ViewBag.Students)
{
@if (ViewBag.Template == "1")
{
<table>
<tr>
<td>
BULK CARD PRINTING
<img src="@ViewBag.QRCodeImage" class="qrcode">
</td>
</tr>
</table>
@*<table>
<tr>
<td>
<div class="wrapper">
<div class="container">
<div style="position: absolute;color: lightgray;opacity: 0.05;font-size: 3em;width: 50%;top: 25%; left: 80px;text-align: center;z-index: 0;background-attachment: fixed;"><img src="~/img/fedpolel-Logo.png" width="150" /></div>
<img src="~/Uploads/Passport//@item.Passport" alt="" class="profile-img">
<img src="~/img/fedpolel-Logo.png" class="logo" />
<img src="@ViewBag.QRCodeImage" class="qrcode">
<div class="content">
<table>
<tr>
<td>
<div class="title">
<h4>THE FEDERAL POLYTECHNIC</h4>
<span>ILE OLUJI, ONDO STATE.</span>
</div>
<div class="sub-content">
<center>
<h1>@item.FullName</h1>
</center>
<p>@item.MatricNumber</p>
</div>
<div class="vr"></div>
<div class="vr-right"></div>
<div class="data">
<div class="inner-data">
<span>Gender</span>
<p>@item.Gender<p />
<br>
<span>Session</span>
<p class="session">@item.Session.SessionName</p>
</div>
<div class="inner-data2">
<span>Level</span>
<p>@item.ClassLevels.ClassLevel</p>
<span>Department</span>
<p>@item.Department.DepartmentName</p>
<span>Faculty</span>
<p>@item.Department.Faculty.FacultyName</p>
</div>
<div class="inner-data3">
<div class="hr"></div>
<p><img src="@ViewBag.Signature" width="50" /></p>
<div class="paragra">Signature</div>
</div>
</div>
</td>
</tr>
</table>
</div>
</div>
</div>
</td>
</tr>
</table>*@
}
else if (ViewBag.Template == "2")
{
@ViewBag.TemplateView
}
else if (ViewBag.Teplate == "3")
{
@ViewBag.TemplateView
}
else
{
@ViewBag.NoTemplateView
}
}
This code only returns data for the last student.
My challenge: if I have 5 students pre-selected, I want to return data of the five students. Please, how do I achieve this?
Upvotes: 0
Views: 709
Reputation: 1176
This is happening because you are always setting ViewBag.Students
to new list in every loop iteration (which contains only one record, because you are filtering by Id).
Initialize ViewBag.Students
outside of loop (e.g. ViewBag.Students = new List<Student>
and inside of loop just add already read student from the database (model.Student
)
e.g.:
ViewBag.Students.Add(model.Student);
Also try to fill model.Students
outside of for loop, since currently you are making unneccessary trip to the databse on each iteration (model.Students = _context.Students.ToList()
). On each for loop iteration you read complete students to the model property, which is redundant. Once should be enough.
I also think that you do not need setting ViewBag's properties to that of a model. Just pass a model (maybe add some needed properties) to the view and read data from the model.
Upvotes: 1