Accordion within table rows - asp.net-mvc-4

I am using Twitter Bootstrap. Currently I am working on Master Details looks alike table, which I am planning to place an accordion into each table's row and if it is expanded, the partial view will be reload and displayed.
Somehow it does not work as expected but if I change it to Modal, the partial view loaded perfectly.
This is the controller that process and returning the values:
public ActionResult Index(int id,int? page)
{
List<CustomerProduct> customerProducts =
(from c in RepositoryProduct.Reload(id)
select c).ToList<CustomerProduct>();
return PartialView("_CustomerProducts", customerProducts);
}
and this is the table in the view :
<table id="tbl" class="table table-condensed" style="border-collapse:collapse;">
<thead>
<tr>
<th>
#Html.ActionLink("Customer Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.DisplayNameFor(model => model.First().CustLocalName)
</th>
<th>
#Html.ActionLink("Country", "Index", new { sortOrder = ViewBag.CountrySortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.DisplayNameFor(model => model.First().City)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr data-toggle="collapse">
<td style="display:none;">
#Html.DisplayFor(modelItem => item.CustID)
</td>
<td>
#Html.DisplayFor(modelItem => item.CustName)
</td>
<td>
#Html.DisplayFor(modelItem => item.CustLocalName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Country.CountryName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
<a class="btn btn-small btn-info" type="button" href="#Url.Action("Index", "CustomerProduct", new { id = item.CustID })" data-toggle="collapse" data-target="##item.CustID" >Products</a>
</td>
</tr>
<tr>
<td colspan="6" class="hiddenRow">
<div id="#item.CustID" class="accordian-body collapse" tabindex="-1">
<div class="accordion-header">
<label>
<strong>#item.CustName product(s)</strong>
</label>
</div>
<div class="accordion-body">TEST</div>
</div>
</td>
</tr>
}
</tbody>
</table>
this is part of the code that calling the controller Index function :
<a class="btn btn-small btn-info" type="button" href="#Url.Action("Index", "CustomerProduct", new { id = item.CustID })" data-toggle="collapse" data-target="##item.CustID" >Products</a>
Actually I would like to load the partialview(_CustomerProduct) in the div class="accordion-body" as what I did in div class="modal-body" but no success. May I know if it is possible to do it and if yes, may I know how?
Any help will be appreciated.

for me to get the view to work i used
<td colspan="6" class="hiddenRow">
<div id="#item.CustID" class="accordian-body collapse" tabindex="-1">
<div class="accordion-header">
<label>
<strong>#item.CustName product(s)</strong>
</label>
</div>
<div class="accordion-body">
#Html.Partial("_CustomerProduct")
</div>
</div>
</td>

Related

ascending order of data from database by Id

I am trying to learn asp.net-core, and I am stuck on trying to retieve the data from database in ascending order of database Id, my code is below but not sure why its not doing it, it does acccessnding order by page but I want all the data in ascending order by database Id, any pointers wold be great...
#model IEnumerable<Citrus.Models.Customers>
#{
ViewData["Title"] = "Index";
Pager pager = new Pager();
int pageNo = 0;
if (ViewBag.Pager !=null)
{
pager = ViewBag.Pager;
pageNo = pager.CurrentPage;
}
}
<form asp-controller="Customers" asp-action="Index">
<p>
<div class="container body-content">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label class="control-label" for="searchString"></label>
<div class="input-group">
<input class="form-control" type="text" name="searchString" placeholder="Search by company name" />
<span class="input-group-btn" name="searchString"><button class="btn btn-default btn-primary mr-1"><i class="fas fa-search"></i> Search</button></span>
#*<span asp-validation-for="searchString"></span>*#
<a class="btn btn-primary" asp-action="Create"><i class="far fa-plus-square"></i> Customer</></a>
</div>
</div>
</div>
</div>
</div>
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Company)
</th>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.Surname)
</th>
<th>
#Html.DisplayNameFor(model => model.Tel)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.OrderByDescending(i => i.Id))
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Company)
</td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tel)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.Id"><i class="fas fa-edit"></i></a> |
<a asp-action="Details" asp-route-id="#item.Id"><i class="fas fa-info-circle"></i></a> |
<a asp-action="Delete" asp-route-id="#item.Id"><i class="fas fa-trash-alt"></i></a>
</td>
</tr>
}
</tbody>
</table>
<div class="container">
#if (pager.TotalPages > 0)
{
<ul class="pagination justify-content-end">
#if (pager.CurrentPage > 1)
{
<li class="page-item">
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="1">First</a>
</li>
<li>
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#(pager.CurrentPage -1)">Previous</a>
</li>
}
#for (var pge = pager.StartPage; pge <= pager.EndPage; pge++)
{
<li class="page-item #(pge == pager.CurrentPage ? "active" : "")">
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#pge"> #pge</a>
</li>
}
#if (pager.CurrentPage < pager.TotalPages)
{
<li class="page-item">
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#(pager.CurrentPage + 1)">Next</a>
</li>
<li>
<a class="page-link" asp-controller="Customers" asp-action="Index" asp-route-pg="#(pager.TotalPages)">Last</a>
</li>
}
</ul>
}
</div>
Many Thank
John
You need to use OrderBy in backend,use OrderBy firstly,and then get the current page data,for example:
List<Citrus.Models.Customers> data = _context.Customers
.OrderBy(c => c.Id)
.Skip(position).Take(pageSize)
.ToList();
For details,you can refer to the official doc.

Is Vue 3 backwards compatible with Vue 2?

In different posts around the internet (for example this one) I found that Vue 2 code should work with Vue 3 but for me it does not. For example I have simple component
<template>
<div class="overflow-x-auto bg-white rounded-lg shadow overflow-y-auto relative">
<table class="table">
<thead>
<tr class="text-left">
<th>
<label>
<input type="checkbox">
</label>
</th>
<th>User ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Gender</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<label>
<input type="checkbox" name="1">
</label>
</td>
<td> <span>{{ id }}</span> </td>
<td> <span>Cort</span> </td>
<td> <span>Tosh</span> </td>
<td> <span>{{ email }}</span> </td>
<td> <span>Male</span> </td>
<td> <span>327-626-5542</span> </td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
id: 1,
email: 'test#mail.com'
}
},
mounted() {
alert('mounted');
},
created() {
alert('created');
}
};
</script>
The table is generated just fine, but both mounted and created not fired when component made. Also the email and id not shown. Only the table is shown.
Your code works:
Vue.createApp({
data() {
return {
id: 1,
email: 'test#mail.com'
}
},
created() {
alert('created')
},
mounted() {
alert('mounted')
}
}).mount('#app')
<script src="https://unpkg.com/vue#next"></script>
<div id="app" class="overflow-x-auto bg-white rounded-lg shadow overflow-y-auto relative">
<table class="table">
<thead>
<tr class="text-left">
<th>
<label>
<input type="checkbox">
</label>
</th>
<th>User ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Gender</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<label>
<input type="checkbox" name="1">
</label>
</td>
<td> <span>{{ id }}</span> </td>
<td> <span>Cort</span> </td>
<td> <span>Tosh</span> </td>
<td> <span>{{ email }}</span> </td>
<td> <span>Male</span> </td>
<td> <span>327-626-5542</span> </td>
</tr>
</tbody>
</table>
</div>

ASP.NET Core - How would I set a name with ID of HTML element on CSHTML

How would I set a name with ID of HTML element on CSHTML?
<tr>
<td>
#item.Items.ItemID
</td>
<td>
#item.Items.ItemModelDescription
</td>
<td class="text-right">
<input id="#item.Items.ItemID + 'UnitPrice'" class="form-control text-right" value="#item.Items.ItemUnitPrice" />
</td>
<td class="text-right">
<input id="#item.Items.ItemID + 'Quantity'" class="form-control text-right" value="#item.Quantity" oninput="return change_quantity('#item.Items.ItemID')"/>
</td>
<td class="text-right">
#(item.Quantity * item.Items.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.Items.ItemID"><span class="fa fa-trash"></span></a>
</td>
</tr>
I can't get the value of HTML element using javascript is there anyway or proper way of setting an id of each quantity input? Or any keywords to search regarding this one.
According to your code and description, I assume you want to calculate the cost based on the Quantity and the ItemUnitPrice. If that is the case, please refer to the following sample code, you can refer it to change your code:
ViewModel:
public class ItemViewModel
{
public int ItemId { get; set; }
public string ItemDescription { get; set; }
public decimal ItemUnitPrice { get; set; }
public decimalc Quantity { get; set; }
}
Index.cshtml: we could set the id attribute like this id='Quantity_#item.ItemId', after rendering, the output like Quantity_XXX:
#model IEnumerable<WebApplication6.Models.ItemViewModel>
<table class="table">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#item.ItemId
</td>
<td>
#item.ItemDescription
</td>
<td class="text-right">
<input id="ItemUnitPrice_#item.ItemId" class="form-control text-right" type="text" value="#item.ItemUnitPrice" />
</td>
<td class="text-right">
<input id='Quantity_#item.ItemId' class="form-control text-right txtquantity" type="text" value="#item.Quantity" oninput="return change_quantity(this,#item.ItemId)" />
</td>
<td class="text-right">
#(item.Quantity * item.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.ItemId"><span class="fa fa-trash"></span></a>
</td>
</tr>
}
</tbody>
</table>
Then at the end of the Index.cshtml page, add the following JavaScript: we could use the parent() and closest() method to find the current row, and then find the relates elements in this row.
#section Scripts{
<script>
function change_quantity(e, itemid) {
//find the item unit price.
var price = $("#" + "ItemUnitPrice_" + itemid).val();
//find the quantity value.
var quantity = $("#" + "Quantity_" + itemid).val();
//calculate the cost and change the html content.
$(e).parent().closest("tr").find(".text-right:last").html((price * quantity).toFixed(2).toString());
}
</script>
}
The output like this:
you can use this approach
#foreach (var item in Model)
{
<tr id="tr-#item.ItemId">
<td>
#item.ItemId
</td>
<td>
#item.ItemDescription
</td>
<td class="text-right">
<input id="ItemUnitPrice_#item.ItemId" class="form-control text-right" type="text" value="#item.ItemUnitPrice" />
</td>
<td class="text-right">
<input id='Quantity_#item.ItemId' class="form-control text-right txtquantity" type="text" value="#item.Quantity" oninput="return change_quantity(this,#item.ItemId)" />
</td>
<td class="text-right">
#(item.Quantity * item.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.ItemId"><span class="fa fa-trash"></span></a>
</td>
</tr>
}

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)

How to change MVC Web Grid structure?

I am trying to show list of items in a MVC 4 Web Grid control. But I am unable to re-format my web grid as table structure below.
Web Grid structure like this.
Table structure like this.
Here is my razor code for web grid.
#{
var grid = new WebGrid(Model.EnrollPlanPriceLevelList, canPage: true, rowsPerPage: 10, selectionFieldName: "id", ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);
if (grid.HasSelection)
{
var EnrollPlanPriceLevelID = grid.Rows[grid.SelectedIndex].Value;
}
}
#grid.GetHtml(
tableStyle: "table table-condensed table-striped table-hover no-margin",
headerStyle: "",
mode: WebGridPagerModes.All,
columns:
grid.Columns(
grid.Column("", format: (item) => item.GetSelectLink(item.EnrollPlanPriceLevelID.ToString())),
grid.Column("SchoolYears.Name", "School Year"),
grid.Column("EnrollPlans.PlanName", "Enroll Plan"),
grid.Column("PriceLevels.Name", "Price Level"),
grid.Column("Price", "Price"),
grid.Column("", format: #<text>#Html.Label("Edit", new { #id = item.EnrollPlanPriceLevelID, #class = "badge", #onclick = "bindplanprogram(" + item.EnrollPlanPriceLevelID + ")" }) </text>),
grid.Column("", format: #<text>#Html.ActionLink("Delete", "Delete", new { id = item.EnrollPlanPriceLevelID }, new { #class = "badge" }) </text>)
))
Here is the table structure
<table class="table table-condensed table-striped table-hover no-margin">
<thead>
<tr>
<th>School Year
</th>
<th>Enroll Plan</th>
<th>
<span class="span3">Early Bird</span>
<span class="span3">Value Pricing</span>
<span class="span4">Standard Pricing</span>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>2011-2012</td>
<td>A(k-8) Grade</td>
<td>
<span class="span3">$ 50</span>
<span class="span3">$ 60</span>
<span class="span3">$ 70</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
<tr>
<td>2011-2012</td>
<td>B (9-12)Grade</td>
<td>
<span class="span3">$ 70</span>
<span class="span3">$ 80</span>
<span class="span3">$ 90</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
<tr>
<td>2011-2012</td>
<td>Adult Ed. (age 18+)</td>
<td>
<span class="span3">$ 1500</span>
<span class="span3">$ 175</span>
<span class="span3">$ 200</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
</tbody>
</table>
I want to reformate my web grid structure as above table structure. Please tell me someone! How to change web grid structure accordingly?
The only way to achieve what you want with the WebGrid is to change the shape of the data you are passing in. Whether you do that in your SQL or whether you do that using LINQ to create a collection of anonymous types that have the appropriate properties is up to you, but the end result must be a collection of items that have public properties that match what you want as column names.