MVC5 razor view dosen't display Bootstrap DataTable with #foreach Loop - asp.net-core

I want to get a line like this with MVC5 #foreach method and dataTables.
Here is the index.cshtml file.
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>
Optionen
</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Position)
</th>
<th>
#Html.DisplayNameFor(model => model.Office)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Position)
</td>
<td>
#Html.DisplayFor(modelItem => item.Office)
</td>
</tr>
}
</tbody>
</table>
#section scripts
{
#Scripts.Render("~/bundles/jquery")
<script>
$(document).ready(function () {
$('#example').DataTable();
});
}
}
The line with 'Show [ ] entries' and 'Search [ ] ' doesen't appear but the rest of the table.
If there is no foreach loop in the table it works. Has someone an idea how to solve the problem?

Yo need to make sure the number of <th> in <tr> is the same with the number of <td> in <tr>.Here is a demo:
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Position)
</td>
<td>
#Html.DisplayFor(modelItem => item.Office)
</td>
</tr>
}
</tbody>
</table>
result:

Related

Select the next data cell given a data cell with Playwright

There are several tables on a page. I'm. trying to get City/Town.
<table class="table" style="margin-top: 35px;">
<thead>
<!--<tr>
<th scope="col"></th>
<th scope="col"></th>
</tr>-->
</thead>
<tbody>
<tr>
<td>Street</td>
<td>61 Essex St</td>
</tr>
<tr>
<td>City/Town</td>
<td>Lynn</td>
</tr>
<tr>
<td>State/Province/Region</td>
<td>Maine</td>
</tr>
<tr>
<td>Zip/Postal Code</td>
<td>01902</td>
</tr>
</tbody>
</table>
I know I can get the element for the cell item above what I need.
let foo = page.locator(`test=City/Town`)
but I'm not sure how to get the next data cell. I googled and tried:
let foo = page.locator(`td:below(:text("street"))`).innerText()
but that didn't work. I also tried:
let foo = page.locator(`td:near(:text'("Street"))+td`).first().textContent()
this didn't work for me either. I tried some other variations.
Can you help a clueless man out?
You can use the adjacent sibling combinator: page.locator('td:text("City/Town") + td').
Example:
const playwright = require("playwright"); // 1.23.3
const html = `<!DOCTYPE html><html><body>
<table class="table" style="margin-top: 35px;">
<thead>
<!--<tr>
<th scope="col"></th>
<th scope="col"></th>
</tr>-->
</thead>
<tbody>
<tr>
<td>Street</td>
<td>61 Essex St</td>
</tr>
<tr>
<td>City/Town</td>
<td>Lynn</td>
</tr>
<tr>
<td>State/Province/Region</td>
<td>Maine</td>
</tr>
<tr>
<td>Zip/Postal Code</td>
<td>01902</td>
</tr>
</tbody>
</table>
</body></html>`;
let browser;
(async () => {
browser = await playwright.chromium.launch();
const page = await browser.newPage();
await page.setContent(html);
const result = await page.locator('td:text("City/Town") + td')
.textContent();
console.log(result); // => Lynn
})()
.catch(err => console.error(err))
.finally(() => browser?.close());

laravel sql group

this is how i fetch my data;
$order_piece = CollectionOrder::select('collection_order.id', 'collection_color.color_name', 'collection_color_size_barcode.size', 'collection_color_size_barcode.id as ccsb_id', DB::raw('SUM(order_piece.piece) As piece'))
->join('order_piece', 'order_piece.order_id', '=', 'collection_order.id')
->join('collection_color', 'collection_color.id', '=', 'collection_order.collection_color_id')
->join('collection_color_size_barcode', 'collection_color_size_barcode.id', '=', 'order_piece.collection_color_size_barcode_id')
->where('collection_order.cut_id', $request->cut_id)
->groupBY('collection_color_size_barcode.size')
->groupBY('collection_order.collection_color_id')
->get();
blade code;
<table class="table table-hover table-light">
<thead>
<tr>
<th> size </th>
<th> piece </th>
</tr>
</thead>
#foreach($order_piece as $op)
<tr>
<td> {{$op->size}} </td>
<td> {{$op->piece}} </td>
</tr>
#endforeach
</table>
the data is like this;
[{"id":12,"color_name":"\u00c7ok Renkli","size":"34","ccsb_id":29,"piece":"82"},{"id":11,"color_name":"K\u0131rm\u0131z\u0131","size":"34","ccsb_id":24,"piece":"82"},{"id":13,"color_name":"\u00c7ok Renkli","size":"36","ccsb_id":30,"piece":"123"},{"id":10,"color_name":"K\u0131rm\u0131z\u0131","size":"36","ccsb_id":25,"piece":"123"},{"id":13,"color_name":"\u00c7ok Renkli","size":"38","ccsb_id":31,"piece":"123"},{"id":10,"color_name":"K\u0131rm\u0131z\u0131","size":"38","ccsb_id":26,"piece":"123"},{"id":12,"color_name":"\u00c7ok Renkli","size":"40","ccsb_id":32,"piece":"82"},{"id":11,"color_name":"K\u0131rm\u0131z\u0131","size":"40","ccsb_id":27,"piece":"82"},{"id":12,"color_name":"\u00c7ok Renkli","size":"42","ccsb_id":33,"piece":"41"},{"id":11,"color_name":"K\u0131rm\u0131z\u0131","size":"42","ccsb_id":28,"piece":"41"}]
I want to list this data by color. but it didn't occur to me. I want to list the size of each color regularly. like the example in the picture, can you help?

ASP.NET MVC4: How to hightlight selected row and send id corresponding to controller when click on edit or detail or delete link

I need help please. I'm using ASP.NET MVC 4. I have a view(Index.cshtml) that display data in table from sql database using entity framework. I want to know how to hightlight selected row and send specific id(id_equipment) to one html.actionlink for editing, deleting or viewing details.
Here's my view:
#model IEnumerable<TEST.Models.equipment>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#Html.ActionLink("Edit", "Edit", new { id = /*item.id_equipment*/ }) |
#Html.ActionLink("Details", "Details", new { id = /*item.id_equipment*/ }) |
#Html.ActionLink("Delete", "Delete", new { id = /*item.id_equipment*/ })
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.id_equipment)
</th>
<th>
#Html.DisplayNameFor(model => model.name)
</th>
<th>
#Html.DisplayNameFor(model => model.type)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.id_equipment)
</td>
<td>
#Html.DisplayFor(modelItem => item.name)
</td>
<td>
#Html.DisplayFor(modelItem => item.type)
</td>
</tr>
}
</table>
Thank you!

How to multiply two columns in a table together to produce a new column

I have 2 fields quantity and price. I basically want to multiply them together and get a value for another column called Price (Which is the total of the multiplication).
I have tried using the below html code:
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
This is my Table row code:
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>
Consider having an additional property in your viewmodel or Model which does this task for you. Then you can bind this with html helper as you did for every other field.
public class YourViewModel
{
public int Field1{ get; set; }
public int Field2{ get; set; }
public int CalculatedField{
get {return Field1*Field2;}
}
}
Or try below code which calculates the value and stores in variable, then renders value directly from variable.
try this
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
var computedValue = item.item_order_quantity*item.ITEM.item_price
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#(computedValue)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>

Issue Saving Edited Data: DDL Value can not be null

I am having an issue with saving the edited fields in my MVC project. When I click the submit button I get an error
Value cannot be null.
Parameter name: items
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items
Source Error:
Line 63:
Line 64:
Line 65: #Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose")
Line 66:
Line 67:
I have tried some things that I have seen on the internet but it doesn't seem to be doing anything for me :( Help would be appreciated thank you!
this is my controller When I put a break point here it never triggers.
[HttpPost]
public ActionResult IndexPST(Event_Setup es)
{
db.Event_Setup.Add(es);
db.SaveChanges();
return View("Index",es);
}
And this is my View
#model DixonGroupInc.Models.Event_Setup
<link href="~/Content/EventManagement.css" rel="stylesheet" />
#using (Html.BeginForm("IndexPST", "EventManagement", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<div id="cntr">
<div id="emLoc">
<table id="ldTbl">
<thead>
<tr>
<th class="ln">
<img src="~/Images/4.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="chosen space">EventMetadata
</td>
<td class="space">Client / Vendor
</td>
<td class="space">Venu info
</td>
<td class="space">Sponsored Participants
</td>
<td class="space">Event Services
</td>
</tr>
</tbody>
</table>
</div>
<div id="emSetup">
Setup Meeting
</div>
<div id="emTable">
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Type")
</td>
<td class="clm2">
#Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose")
</td>
<td class="clm3">
#{Html.RenderAction("EventType", "EventManagement");}
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Event Title")
</td>
<td class="clm2">
#Html.EditorFor(model => model.EventTitle)
#Html.ValidationMessageFor(model => model.EventTitle)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Identifier")
</td>
<td class="clm2">
#Html.EditorFor(model => model.EventIdentifier)
#Html.ValidationMessageFor(model => model.EventIdentifier)
</td>
</tr>
</table>
<table class="dateTbl">
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Event Date From")
</td>
<td class="clm2">
#Html.TextBoxFor(model => model.EventDateFrom, new { #class = "dateFrom" })
#Html.ValidationMessageFor(model => model.EventDateFrom)
</td>
<td class="lblEvent">
#Html.Label("To")
</td>
<td class="clm4">
#Html.TextBoxFor(model => model.EventDateTo, new { #class = "dateTo" })
#Html.ValidationMessageFor(model => model.EventDateTo)
</td>
</tr>
</table>
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Description")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.EventDescription, new
{
id = "taED"
})
<div>
<span id="charLeft2"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.EventDescription)
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Custom Message")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.CustomMessage, new
{
id = "taCM"
})
<div>
<span id="charLeft1"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.CustomMessage)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
#Html.Label("Instructions")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.Instructions, new
{
id = "taInstrct"
})
<div>
<span id="charLeft"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.Instructions)
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td>
#{Html.RenderAction("AddTrack", "EventManagement");}
</td>
</tr>
<tr>
<td>
#{Html.RenderAction("EventTrack", "EventManagement");}
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("CFP Pocess")
</td>
<td class="clm2">
#Html.RadioButtonFor(model => model.CFPRequired, "true") Yes
</td>
<td class="clm3">
#Html.RadioButtonFor(model => model.CFPRequired, "false") No
</td>
</tr>
</table>
</div>
<div id="emSubmit">
<input type="submit" value="Save & Next" id="submit" />
</div>
</div>
}
Viewbag can be unreliable so generally it isn't recommended to pass your drop down list data through the viewbag. I would recommend adding it to your view model instead. In your Event_Setup class make sure that you have
public int EventTypeID { get; set; }
and
public List<SelectListItem> EventTypeList { get; set; }
on your controller get you can set EventTypeID to set a default on the drop down so your controller should have something like
Event_Setup es = new Event_Setup();
es.EventTypeID = //default value if you want;
es.EventTypeList = //Method call here where List<SelectListItem> is returned with your event list
then on your view you can change your drop down to
#Html.DropDownListFor(x => x.EventTypeID, Model.EventTypeList)