Telerik MVC Grid does not display data - telerik-grid

I am using version 2011.2.914 of the controls. Whenever I try to bind data from my controller to a view, I can't get the data inside the collection to display on the grid. I get the "No records to display" message from the grid.
I want to point out that if I am not using the Telerik grid, the data comes out fine (see below):
Controller without Telerik Grid
IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
return View("Index", customerList);
View
#model IEnumerable<YeagerTech.YeagerTechWcfService.Customer>
#{
ViewBag.Title = "Customer Index";
}
<h2>Customer Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Email
</th>
<th>
Company
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Address1
</th>
<th>
Address2
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Zip
</th>
<th>
HomePhone
</th>
<th>
CellPhone
</th>
<th>
Website
</th>
<th>
IMAddress
</th>
<th>
CreatedDate
</th>
<th>
UpdatedDate
</th>
<th>
</th>
</tr>
#if (ViewData.Model != null)
{
foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Email)
</td>
<td>
#Html.DisplayFor(modelItem => item.Company)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address2)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#Html.DisplayFor(modelItem => item.State)
</td>
<td>
#Html.DisplayFor(modelItem => item.Zip)
</td>
<td>
#Html.DisplayFor(modelItem => item.HomePhone)
</td>
<td>
#Html.DisplayFor(modelItem => item.CellPhone)
</td>
<td>
#Html.DisplayFor(modelItem => item.Website)
</td>
<td>
#Html.DisplayFor(modelItem => item.IMAddress)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.UpdatedDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.CustomerID }) |
#Html.ActionLink("Details", "Details", new { id = item.CustomerID })
</td>
</tr>
}
}
</table>
I have tried ServerSide and Ajax Binding. Both don't work. I have searched the Telerik site and the web and can't find out what is missing.
What do I need to do in order to resolve this and get the data in my grid?
Here is the code in my Customer controller for the Ajax binding (which is what I really want). Upon return of getting the data, I have an image file that I saved where the data is in the customerList collection (there is no option to attach any files in StackOverflow, so I can't show you).
When the View is being rendered, I captured data while debugging for several objects.
When cycling through the grid during the binding, I captured data in another file.
Controller using Telerik Grid:
using Telerik.Web.Mvc;
public class CustomerController : Controller
{
[GridAction]
public ActionResult Index()
{
IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
return View(new GridModel<YeagerTechWcfService.Customer>
{
Data = customerList
});
}
}
Below, is the code in my associated View. btw, all the bound columns have intellisense that pop up for the columns in my model. This is for the Index action within the Customer controller.
#model Telerik.Web.Mvc.GridModel<YeagerTech.YeagerTechWcfService.Customer>
#{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
#(Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>()
.Name("Customers")
.Columns(columns =>
{
columns.Bound(o => o.Email);
columns.Bound(o => o.Company);
columns.Bound(o => o.FirstName);
columns.Bound(o => o.LastName);
columns.Bound(o => o.Address1);
columns.Bound(o => o.Address2);
columns.Bound(o => o.City);
columns.Bound(o => o.State);
columns.Bound(o => o.Zip);
columns.Bound(o => o.HomePhone);
columns.Bound(o => o.CellPhone);
columns.Bound(o => o.Website);
columns.Bound(o => o.IMAddress);
columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}");
columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}");
}).DataBinding(dataBinding => dataBinding.Ajax().Select("Index", "Customer"))
.Pageable()
.Sortable()
.Scrollable()
.Resizable(resizing => resizing.Columns(true))
.Filterable())
</table>

Maybe there's a conflic with your Index action method since that you have a View that is referenced to it. The first test to do is to show your grid without playing with Ajax or anything else.
Try this to initialize your grid when your View Index is build. Notice the Model.Data that i'm using in the Grid constructor:
#(Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>(Model.Data)
.Name("Customers")
.Columns(columns =>
{
columns.Bound(o => o.Email);
columns.Bound(o => o.Company);
...
}))
If it work, then you should try to create another Action method with another name. In my projects, i do this:
[GridAction]
public ActionResult GetYeagerTechGridData()
{
IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
return View(new GridModel<YeagerTechWcfService.Customer>
{
Data = customerList
});
}
and use your grid that way:
#(Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>()
.Name("Customers")
.Columns(columns =>
{
columns.Bound(o => o.Email);
columns.Bound(o => o.Company);
...
})
.DataBinding(dataBinding => dataBinding.Ajax().Select("GetYeagerTechGridData", "Customer")))

Related

Map array dont show in table

I need map a array inside my page, and show the result in a table, but the content don't show up when I compiled the page.
Can anyone help me?
When I print in console the content of a var, this is here. But the info don't show up in the page
import Layout, { siteTitle } from '../components/layout'
const fetch = require('node-fetch');
export default function Home({ devices }) {
return (
<Layout >
{devices.map((device) => (
<table>
<thead>
<th>
{device.localname} / {device.localIP}
</th>
</thead>
{console.log('1')}
<tbody>
<tr>
<td>
{device.IPaddress[0][3].value} // I put this to test, and this works fine
</td>
</tr>
{device.IPaddress.map((port) =>{
<tr>
<td>
{console.log(port[3].value), port[3].value} // here I need to work, I want to put all results of port in a TD tag, the console.log shows up the info, but the page not.
</td>
</tr>
})}
</tbody>
</table>
))}
</Layout >
)
}
export async function getStaticProps() {
const res = await fetch('http://localhost:3000')
const devices = await res.json()
return {
props: {
devices
}
}
}
As commented by #evgenifotia, change the ( for { inside the second array map works fine.
here the final function:
export default function Home({ devices }) {
return (
<Layout >
{devices.map((device) => (
<table>
{console.log('1')}
<tbody>
<tr>
<th>
{device.localname} / {device.localIP}
</th>
</tr>
{device.IPaddress.map(port =>(
<tr>
<td>
{console.log(port[3].value), port[3].value}
</td>
</tr>
))}
</tbody>
</table>
))}
</Layout >
)
}

How to create drop down list in excel using laravel-excel or phpspreadsheet?

I am using laravel-excel 3.0 in my laravel project and i am unable to create drop-down list while downloading the list.
Below is Export class code
class MasterFields implements FromView
{
public function view(): View
{
return view('master-fields', [
'master_fields' => [
[
'name' => 'Povilas',
'surname' => 'Korop',
'email' => 'povilas#laraveldaily.com',
'twitter' => ['#povilaskorop', 'test', 'test']
],
[
'name' => 'Taylor',
'surname' => 'Otwell',
'email' => 'taylor#laravel.com',
'twitter' => ['#povilaskorop', 'test', 'test']
]
]
// MasterDataField::get_master_data_set()
]);
}
}
Below is blade code
<table>
<thead>
<tr>
<th style="color:blue;"><b> ID </b></th>
<th style="color:blue;"><b> Field Name </b></th>
<th style="color:blue;"><b> Data Type </b></th>
</tr>
</thead>
<tbody>
#foreach($master_fields as $field)
<tr>
<td>{{ $field['name'] }}</td>
<td>{{ $field['surname'] }}</td>
<td>
<select>
#foreach($field['twitter'] as $twitter)
<option> {{ $twitter }} </option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
I am using below method to download
return Excel::download(new MasterFields, 'test.xlsx');
As you mentioned the old way of definition doesn't work. But here is an example of the new way: https://laraveldaily.com/laravel-excel-3-0-export-custom-array-excel/

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)

Edit in view page or index.cshtml in MVC

I am using MVC 5 and Entity framework 6 (Database First) in my application.
I need to update "value" field in View page\index.cshtml.
Is it possible to use #Html.EditorFor to edit Value field for each Item on single button click?
Here is the code in index.cshtml
<table>
<tr>
<th>
Number
</th>
<th>
Name
</th>
<th>
Value
</th>
<th>
Description
</th>
</tr>
foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.number)
</td>
<td>
#Html.DisplayFor(modelItem => item.name)
</td>
<td>
#Html.EditorFor(modelItem => item.value)
#*#Html.ValidationMessageFor(modelItem => item.value) *#
</td>
<td>
#Html.DisplayFor(modelItem => item.description)
</td>
#* <td>
#Html.ActionLink("Edit", "Edit", new { id=item.id })
</td>*#
</tr>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
On clicking Save button I should update the data in Value field for any number of rows.
It's done in Database First so action for Edit,View,Details are included in Controller.
You'll have to put everything in a to post the details to the server, then you can manage the posted elements. You'll post a list of itens. Maybe this will help you: How can I post a list of items in MVC

Export Aspx View Page HTML Table Data to Excel File

In my MVC3 Web application I have a aspx main view page and a partial View Page. My table is in Partial view. I have also applied Table sorter and other table css to it. Now I want to export data from this table to excel. I tried so many ways but it didn't worked for me. Can anyone Help Me Please...
Code of my partial view is:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<ApricaCRMEvent.Models.CRM.DatabaseEntities.CRM_Doctor_Request>>" %>
<!-- for table sorter and table Pager -->
<script src="../../__jquery.tablesorter/jquery.tablesorter.js" type="text/javascript"></script>
<script src="../../__jquery.tablesorter/jquery.tablesorter.widgets.js" type="text/javascript"></script>
<script src="../../__jquery.tablesorter/jquery.tablesorter.pager.js" type="text/javascript"></script>
<script type ="text/javascript" id="js">
$(function () {
// **********************************
// Description of ALL pager options
// **********************************
var pagerOptions = {
// target the pager markup - see the HTML block below
container: $(".pager"),
// use this url format "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
ajaxUrl: null,
// process ajax so that the data object is returned along with the total number of rows
// example: { "data" : [{ "ID": 1, "Name": "Foo", "Last": "Bar" }], "total_rows" : 100 }
ajaxProcessing: function (ajax) {
if (ajax && ajax.hasOwnProperty('data')) {
// return [ "data", "total_rows" ];
return [ajax.data, ajax.total_rows];
}
},
// output string - default is '{page}/{totalPages}'
// possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
output: '{startRow} to {endRow} ({totalRows})',
// apply disabled classname to the pager arrows when the rows at either extreme is visible - default is true
updateArrows: true,
// starting page of the pager (zero based index)
page: 0,
// Number of visible rows - default is 10
size: 10,
// if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty
// table row set to a height to compensate; default is false
fixedHeight: true,
// remove rows from the table to speed up the sort of large tables.
// setting this to false, only hides the non-visible rows; needed if you plan to add/remove rows with the pager enabled.
removeRows: false,
// css class names of pager arrows
cssNext: '.next', // next page arrow
cssPrev: '.prev', // previous page arrow
cssFirst: '.first', // go to first page arrow
cssLast: '.last', // go to last page arrow
cssGoto: '.gotoPage', // select dropdown to allow choosing a page
cssPageDisplay: '.pagedisplay', // location of where the "output" is displayed
cssPageSize: '.pagesize', // page size selector - select dropdown that sets the "size" option
// class added to arrows when at the extremes (i.e. prev/first arrows are "disabled" when on the first page)
cssDisabled: 'disabled' // Note there is no period "." in front of this class name
};
// Extend the themes to change any of the default class names ** NEW **
$.extend($.tablesorter.themes.jui, {
// change default jQuery uitheme icons - find the full list of icons here: http://jqueryui.com/themeroller/ (hover over them for their name)
table: 'ui-widget ui-widget-content ui-corner-all', // table classes
header: 'ui-widget-header ui-corner-all ui-state-default', // header classes
footerRow: '',
footerCells: '',
icons: 'ui-icon', // icon class added to the <i> in the header
sortNone: 'ui-icon-carat-2-n-s',
sortAsc: 'ui-icon-carat-1-n',
sortDesc: 'ui-icon-carat-1-s',
active: 'ui-state-active', // applied when column is sorted
hover: 'ui-state-hover', // hover class
filterRow: '',
even: 'ui-widget-content', // odd row zebra striping
odd: 'ui-widget-content' // even row zebra striping
});
// call the tablesorter plugin and apply the ui theme widget
$("table").tablesorter({
theme: 'jui', // theme "jui" and "bootstrap" override the uitheme widget option in v2.7+
headerTemplate: '{content} {icon}', // needed to add icon for jui theme
// widget code now contained in the jquery.tablesorter.widgets.js file
widgets: ['uitheme', 'zebra'],
widgetOptions: {
// zebra striping class names - the uitheme widget adds the class names defined in
// $.tablesorter.themes to the zebra widget class names
zebra: ["even", "odd"]
// set the uitheme widget to use the jQuery UI theme class names
// ** this is now optional, and will be overridden if the theme name exists in $.tablesorter.themes **
// uitheme : 'jui'
}
})
// bind to pager events
// *********************
.bind('pagerChange pagerComplete pagerInitialized pageMoved', function (e, c) {
var msg = '"</span> event triggered, ' + (e.type === 'pagerChange' ? 'going to' : 'now on') +
' page <span class="typ">' + (c.page + 1) + '/' + c.totalPages + '</span>';
$('#display')
.append('<li><span class="str">"' + e.type + msg + '</li>')
.find('li:first').remove();
})
// initialize the pager plugin
// ****************************
.tablesorterPager(pagerOptions);
});
</script>
<!-- end table sorter and table Pager -->
<table class="tablesorter" id="ResultList">
<thead id="headers">
<tr>
<th>
CRMId
</th>
<th>
Territory
</th>
<th>
Doctor(MDLNo)
</th>
<th>
Requester
</th>
<th>
Suggester
</th>
<th>
Division
</th>
<th>
Estimated
</th>
<th>
Actual
</th>
<th>
Priority
</th>
<th>
Notation
</th>
<th>
Deadline
</th>
<th>
ExecutedDate
</th>
<th>
CreatedDate
</th>
<th>
CRM State
</th>
<th>
Service State
</th>
</tr>
</thead>
<tbody>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink(((int)item.Id).ToString(), "DetailsCRMRequest", "CRM", new { Id = item.Id, controllerName = "FilterCRMRequest", actionName = "Index" }, null)%>
<%--<%: Html.DisplayFor(modelItem => item.Id) %>--%>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Territory_Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Request_For_Name) %> (<%: Html.DisplayFor(modelItem => item.Request_For_Id) %>)
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Requester_Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Suggester_Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Division_Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Estimated_Amount) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Actual_Amount) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Compute_Priority) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.CRM_Notation_Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Deadline) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Executed_Date) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Date_Created) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Compute_CRM_State) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Compute_Service_State) %>
</td>
</tr>
<% } %>
</tbody>
<tfoot>
<tr class="pager" align="center">
<td colspan="15">
<img src="../../Content/images/first.png" class="first" alt="First" />
<img src="../../Content/images/prev.png" class="prev" alt="Prev" />
<span class="pagedisplay"></span> <!-- this can be any element, including an input -->
<img src="../../Content/images/next.png" class="next" alt="Next" />
<img src="../../Content/images/last.png" class="last" alt="Last" />
<select class="pagesize" title="Select page size">
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
<select class="gotoPage" title="Select page number"></select>
</td>
</tr>
</tfoot>
</table>
I have already tried this : this works but gives me css in Excel file and 5 td fields from all 50 records result...
function Export(e) {
window.open('data:application/vnd.ms-excel,' + $('#ResultList').html());
e.preventDefault();
}
and this : it doesn't worked totally...
function Export() {
alert("called function");
str = "";
var myTableHead = document.getElementById('headers');
var rowCount = myTableHead.rows.length;
var colCount = myTableHead.getElementsByTagName("tr")[0].getElementsByTagName("th").length;
var ExcelApp = new ActiveXObject("Excel.Application");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;
alert("before for");
for (var i = 0; i < rowCount; i++) {
for (var j = 0; j < colCount; j++) {
str = myTableHead.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].innerHTML;
ExcelSheet.ActiveSheet.Cells(i + 1, j + 1).Value = str;
}
}
alert("done");
}

Resources