How i show ViewBag value in my view page? - asp.net-mvc-5

I have two class. Such as:
My controller code is shown below:
public class ResultController : Controller
{
private OnlineVotingSystemEntities db = new OnlineVotingSystemEntities();
// GET: Result
public ActionResult result()
{
var vp = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "VP"
orderby c.Vote descending
select new {
UserPhoto = v.UserPhoto,
UserFirstName =v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.VP1 = vp;
var gs = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "GS"
orderby c.Vote descending
select new
{
UserPhoto = v.UserPhoto,
UserFirstName = v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.GS1 = gs ;
var ags = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "AGS"
orderby c.Vote descending
select new
{
UserPhoto = v.UserPhoto,
UserFirstName = v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.AGS1 = ags;
var member = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "Member"
orderby c.Vote descending
select new
{
UserPhoto = v.UserPhoto,
UserFirstName = v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.Member1 = member;
return View();
}
}
My View Page is shown below:
#using OnlineVoting.Models
#{
ViewBag.Title = "result";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>result</h2>
<div class="container">
<h4>VP</h4>
<table cellpadding="2" cellspacing="2" border="1" class="dataTable">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
<th>Vote</th>
</tr>
#foreach (var item in ViewBag.VP1)
{
<tr>
<td><p><img src="data:image;base64,#Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>#item.UserFirstName</td>
<td>#item.UserRegNo</td>
<td>#item.Vote</td>
</tr>
}
</table>
</div>
<div class="container">
<h4>GS</h4>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
<th>Vote</th>
</tr>
#foreach (var item in ViewBag.GS1)
{
<tr>
<td><p><img src="data:image;base64,#Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>#item.UserFirstName</td>
<td>#item.UserRegNo</td>
<td>#item.Vote</td>
</tr>
}
</table>
</div>
<div class="container">
<h4>AGS</h4>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
<th>Vote</th>
</tr>
#foreach (var item in ViewBag.AGS1)
{
<tr>
<td><p><img src="data:image;base64,#Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>#item.UserFirstName</td>
<td>#item.UserRegNo</td>
<td>#item.Vote</td>
</tr>
}
</table>
</div>
<div class="container">
<h4>MEMBER</h4>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
#*<th>Vote</th>*#
</tr>
#foreach (var item in ViewBag.Member1)
{
<tr>
<td><p><img src="data:image;base64,#Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>#item.UserFirstName</td>
<td>#item.UserRegNo</td>
<td>#item.Vote</td>
</tr>
}
</table>
</div>
When I run this code then show the output:
Image 3 show the output of this code. I got an error. This error is : object does not contain a definition for UserPhoto. Why I got this type of error? Please help me.

In your controller, you are using anonymous object but I am suggesting to you to use strongly typed object and since you are using Viewbag, no need to cast in view. Like `
var vp = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "VP"
orderby c.Vote descending
select new CandiateViewModel{
UserPhoto = v.UserPhoto,
UserFirstName =v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();

Related

NodeJS template literal with async map

I'm trying to generate an HTML content using template literal because I need to loop through an array. It is not returning the map content to the template literal.
const htmlData = `
<html>
... // Some content here
<table class="table table-bordered" style="width: 800px;" align="center">
<thead class="thead-light">
<tr>
<th>Company</th>
<th>Bill Date</th>
<th>Billing Days</th>
<th>Total Units</th>
</tr>
</thead>
<tbody>
${customers.map( async (customer) => {
let units = 0;
const companyDate = new Date(customer.CompanyStartDate);
const billDate = new Date(today.getFullYear(), today.getMonth(), companyDate.getDate());
const numDays = (today.getTime() - billDate.getTime()) / (1000 * 3600 * 24);
const pData = await property.getPropTotalUnitsByCompany(customer.CompanyID);
return (
`
<tr>
<td>
${pData.map(async (propData) => {
units += propData.TotalUnits;
return (
`
<div class="row">
<div class="col-sm-12" align="right">
Property: ${propData.PropertyName} Total Units: ${propData.TotalUnits}
</div>
</div>
`
);
})}
</td>
<td>${myFunctions.formatDisplayDate(billDate)}</td>
<td>${numDays}</td>
<td>${units}</td>
</tr>
`
);
})}
</tbody>
</table>
... // More content here
</html>
`;
It returns [object Promise] inside my . Here is where I have the .map(). Can't I use map in template literals?
Thank you

Jquery datatable not working for first row in colspan

I tried to bind dynamically partial view its working but for jquery
datatable against first row what I specified below its not working.
#model IEnumerable<PurModelClass>
<table id="stock-audit-table" class="table table-striped table-bordered dt-responsive nowrap">
<thead>
<tr>
#*<th>Purchase From</th>
<th>Purchase Date</th>*#
<th>Item Name</th>
<th>Qty</th>
<th>Unit</th>
<th>Cost</th>
<th>Net Amount</th>
<th>Discount</th>
<th>Vat</th>
<th>Gross</th>
</tr>
</thead>
<tbody id="stock-audit-tbody">
#foreach (var item in Model)
{
<tr><td colspan="8">Invoice No: #item.PurInvNo Inv Date: #($"{#item.PurDate:MMM-dd-yyyy}") Vendor: #item.VendorName </td></tr>
foreach (var detail in item.PurDetails)
{
<tr>
<td>#detail.StockName</td>
<td>#detail.PurQty</td>
<td>#detail.UnitName</td>
<td>#detail.UnitPrice</td>
<td>#detail.Total</td>
<td>#detail.Discount</td>
<td>#detail.VAT</td>
<td>#detail.NetAmount</td>
</tr>
}
}
</tbody>
<tfoot>
</tfoot>
</table>
This the datatable script, is there any way to implement jquery datatable by demanding specific row with colspan properties.
<script type="text/javascript">
$(document)
.ready(function() {
$('#stock-audit-table').DataTable({
"autoWidth": false,
"order": [[0, "desc"]]
});
}
);
</script>
Try the rowGroup extension. You can use startRender and endRender to customize the summary data
$('#stock-audit-table').DataTable({
"autoWidth": false,
"order": [[0, "desc"]],
"rowGroup": {
dataSrc: 0,
startRender: function(groupName){
return $('<tr/>').append("<td colspan="8">TODO put data here</td>");
}
}
});
<tbody id="stock-audit-tbody">
#foreach (var item in Model)
{
foreach (var detail in item.PurDetails)
{
<tr>
<td>
<!-- Define a key to be referenced by the
'datasrc' property in dataTables -->
#item.GroupName
</td>
<td>#detail.StockName</td>
<td>#detail.PurQty</td>
<td>#detail.UnitName</td>
<td>#detail.UnitPrice</td>
<td>#detail.Total</td>
<td>#detail.Discount</td>
<td>#detail.VAT</td>
<td>#detail.NetAmount</td>
</tr>
}
}
</tbody>

How to parse xml using groovy

I'm new to groovy xml parsing. I'm trying to parse the below xml file
<font face=Tahoma size=2>
Team,<br/><br/> Please find below the test summary details for the 'Test' execution.<br/><br/><b><U>Transaction Summary Table:</U></b><br/><br/>
<table border=1 CELLPADDING =3 style='font-family:Tahoma;font-size:12'>
<tr>
<b>
<th bgcolor=#C0C0C0> TransactionName </th>
<th bgcolor=#C0C0C0> AverageLatency </th>
<th bgcolor=#C0C0C0> MinimumLatency </th>
<th bgcolor=#C0C0C0> MaximumLatency </th>
<th bgcolor=#C0C0C0> AverageElapsedTime </th>
<th bgcolor=#C0C0C0> MinimumElapsedTime </th>
<th bgcolor=#C0C0C0> MaximumElapsedTime </th>
<th bgcolor=#C0C0C0> TotalCount </th>
<th bgcolor=#C0C0C0> PassPercentage </th>
</b>
</tr>
<tr>
<td>1 /aumentum/</td>
<td>
<center>1648.0</center>
</td>
<td>
<center>1240</center>
</td>
<td>
<center>2900</center>
</td>
<td>
<center>1907.0</center>
</td>
<td>
<center>1495</center>
</td>
<td>
<center>3140</center>
</td>
<td>
<center>45</center>
</td>
<td>
<center>100.0</center>
</td>
</tr>
<tr>
<td>T01_Aumentum_Home</td>
<td>
<center>6.0</center>
</td>
<td>
<center>1</center>
</td>
<td>
<center>10</center>
</td>
<td>
<center>1956.0</center>
</td>
<td>
<center>1490</center>
</td>
<td>
<center>3806</center>
</td>
<td>
<center>213</center>
</td>
<td>
<center>0.0</center>
</td>
</tr>
</tbody>
</table>
<br/><br/>Thanks,<br/>Performance Team.
</font>
<br/><br/>
Expected Result:
[{
"transaction name":"1 /aumentum/",
"AverageLatency ":"1648.0",
"Minimum latency":"1240",
"MaximumLatency ":"2900",
"AverageElapsedTime":"1907.0",
"MinimumElapsedTime":"1495",
"MaximumElapsedTime":"3140",
"TotalCount":"45",
"PassPercentage":"100.0"
},
{
"transaction name": "1 /aumentum/",
"AverageLatency ":"1648.0",
"Minimum latency":"1240",
"MaximumLatency ":"2900",
"AverageElapsedTime":"1907.0",
"MinimumElapsedTime":"1495",
"MaximumElapsedTime":"3140",
"TotalCount":"45",
"PassPercentage":"100.0"
}]
i have got the first children using values using docParser.getElementsByTag("tr").first()
Here is the error I get:
Exception thrown
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at org.jsoup.select.Elements.get(Elements.java:519)
at org.jsoup.nodes.Element.child(Element.java:174)
at org.jsoup.nodes.Element$child$0.call(Unknown Source)
at CommonUtils.parseLRHTMLReport(jmeteragent.groovy:304)
at CommonUtils$parseLRHTMLReport.call(Unknown Source)
Here is what I have done so far:
def transactiondetails12 = null
def iterator12 = 0
int count1 = 0
def violcounts = 0
def violations = null;
tmpElement = docParser.getElementsByTag("tr").first()
println tmpElement.children()
// tmpElement= tmpElement.child(0)
// println "#########tmpElement#########:" +tmpElement
for (element in tmpElement.children()) {
if (iterator12 == 0) {
// transactiondetails1 = "<table border=1 CELLPADDING =3 style='font-family:Tahoma;font-size:12'><tr><b><th bgcolor=#C0C0C0>" +
element.child(0).text().trim() + "</th><th bgcolor=#C0C0C0>" + element.child(2).text().trim() + "</th><th bgcolor=#C0C0C0>" +
element.child(3).text().trim() + "</th><th bgcolor=#C0C0C0>" + element.child(4).text().trim() + "</th></b></tr>"
iterator12 = 1;
count1++;
// println "nqwlieufrh 2938ry `9p23dhWCDNJ p3fu89 Q2390RUD"+transactiondetails1
} else {
count1++;
if (count1 <= 5) {
// println "iterator1iterator1iterator1iterator1"+iterator1++
transactiondetails12 = transactiondetails12 + "<tr><td>" + element.child(0).text().trim() + "</td><td><center>" +
element.child(2).text().trim() + "</center></td><td><center>" +
element.child(3).text().trim() + "</center></td><td><center>" +
element.child(4).text().trim()
println "transactiondetails12" + transactiondetails12
// println "3215463654156436212315465123011482145634217225445622341"+element.child(4).text().trim()
String violation1 = element.child(1).text()
// violation=Integer.valueOf(violation1)
// violation=Integer.parseInt(violation1)
// if(violation1>=0)
if (violation1.length() > 0) {
violcounts++
}
}
}
}
I have no idea how to map the tmpElement.children() values. Any advise on this would be helpful. Thanks in advance.
The sample you have provided uses jsoup library that is useful for HTML DOM manipulation. The solution to your problem is to use correct selectors to extract the data.
Consider following example:
def headers = docParser.select("tr > th").collect { it.text() }
def result = []
docParser.select("tr:has(td)").each { tr ->
def obj = [:]
tr.select("td").eachWithIndex { Element td, int i ->
obj[headers[i]] = td.text()
}
result << obj
}
println JsonOutput.prettyPrint(JsonOutput.toJson(result))
docParser.select("tr > th").collect { it.text() } collects table headers and stores them as an ordered List<String>
docParser.select("tr:has(td)") selects all rows (excluding table header) with data
tr.select("td").eachWithIndex iterates inside each row, collects the data and associates it with header by index i
the last line displays desired output to console
Output:
[
{
"TransactionName": "1 /aumentum/",
"AverageLatency": "1648.0",
"MinimumLatency": "1240",
"MaximumLatency": "2900",
"AverageElapsedTime": "1907.0",
"MinimumElapsedTime": "1495",
"MaximumElapsedTime": "3140",
"TotalCount": "45",
"PassPercentage": "100.0"
},
{
"TransactionName": "T01_Aumentum_Home",
"AverageLatency": "6.0",
"MinimumLatency": "1",
"MaximumLatency": "10",
"AverageElapsedTime": "1956.0",
"MinimumElapsedTime": "1490",
"MaximumElapsedTime": "3806",
"TotalCount": "213",
"PassPercentage": "0.0"
}
]
And here you can find full Groovy script I've used for experimenting with your example: https://gist.github.com/wololock/651a536dff4e104ebba0eef69d4ac3ea
I hope it helps.

Creating a product gallery with EF 6 and MVC 5

I'm working on a website for someone and am having a small issue. I'll admit I'm a little new to MVC and EF so I've got this issue. My Index view shows my products, but it shows them in a straight up and down list, like the table I created it supposed to do.
What I'm trying to do is create a gallery, where they're side by side for say 4 in a row, then move on to the next row and so forth (God I know this is making sense). Here's my view
#model IEnumerable<AccessorizeForLess.ViewModels.DisplayProductsViewModel>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)<br />
<img src="#item.Image.ImagePath"/>
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
#Html.ActionLink("Details", "Details", new { id = item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
And of course it's doing what it's supposed to do. Can someone help me out with this?
In case anyone has anything close to this issue this is what I came up with and it works just the way I was looking for:
#model IEnumerable<AccessorizeForLess.ViewModels.DisplayProductsViewModel>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Products</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#foreach (var item in Model)
{
<div style="float:left; width:25%">
#Html.DisplayFor(modelItem => item.Name)<br />
<input id="Hidden1" type="hidden" value="#item.Id" />
<div>$#Html.DisplayFor(modelItem => item.Price)</div>
<div><img src="#item.Image.ImagePath" /></div>
<div> </div>
<div>Quantity: <input type="text" id="quantity" style="width:50px;" /></div>
<div>#Html.ActionLink("Details", "Details", new { id = item.Id })</div>
</div>
}
I do, however have one question, in my controller how can I format the price as currency, I tried this:
IEnumerable<DisplayProductsViewModel> model = products.Select(p => new DisplayProductsViewModel()
{
Id = p.ProductId,
Name = p.ProductName,
Image = p.ProductImage,
Price = string.Format("{0:C}",p.ProductPrice)
}).ToList();
And get the error:
LINQ to Entities does not recognize the method 'System.String
Format(System.String, System.Object)' method, and this method cannot
be translated into a store expression

Pass Id to ActionMethod

I have a Delete button inside my table and I m trying to delete the selected Row.
The problem is that I always get in the post method a null ID
<div>
<table class="table">
<thead>
<tr>
<th>Role Name</th>
<th>Action</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#using (Html.BeginForm("Delete","Role", new { id = item.Id },FormMethod.Post))
{
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
</div>
}
</td>
</tr>
}
</table>
In My Controller
// POST: Jobs/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult Delete(int id)
{
IdentityRole role = _context.Roles.Find(id);
_context.Roles.Remove(role);
_context.SaveChanges();
return RedirectToAction("Index");
}
Any time I click on the button the id is null
From your comments, the html generated in <form> tags is
action="/Role/Delete/b1bc13ca-a855-48b0-90e2-9e5fc081ac86"
meaning that the Id property of your model is typeof Guid. Change the POST method signature to
public ActionResult Delete(Guid id)

Resources