Hi,
I want to display a chart based on 2 date parameters but have an error like this, I have tried to solve it but until now I have not found a solution. Please help.
Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List<lma_apps_v._01.Models.TblTimeSheetDt>' to 'lma_apps_v._01.Models.TblTimeSheetDt'
<input type="text" name="tgl1" autocomplete="on" />
<br />
<input type="text" name="tgl2" autocomplete="on" />
<br />
<button type="button" id="ShowChart" class="btn btn-success">Show Chart</button>
<div>
<canvas id="myChart" width="400" height="400">
</canvas>
</div>
<script src="~/chart.js/chart.min.js"></script>
<script src="~/chart.js/chart.umd.min.js"></script>
<script src="~/chart.js/chart.js"></script>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var myChart; // Variabel untuk menyimpan instance Chart
$('#ShowChart').click(function () {
// Hancurkan chart jika sudah ada
if (myChart) {
myChart.destroy();
}
$.ajax({
url: '/TimeSheetDt/GetDataChartTSDT',
type: 'POST',
contentType: 'application/json;charset:utf8',
dataType: 'json',
data: '',
success: OnSuccessResult,
error: OnErrorResult
});
function OnSuccessResult(data) {
var _data = data;
var _chartLabel = _data[0];
var _chartData = _data[1];
var barColor = ['green'];
// Buat chart baru setelah destroyed yang lama
myChart = new Chart('myChart', {
type: 'bar',
data: {
labels: _chartLabel,
datasets: [{
backgroundColor: barColor,
data: _chartData
}]
}
});
}
function OnErrorResult(xhr, status, error) {
// Tampilkan pesan kesalahan atau lakukan tindakan lain
console.log('Error: ' + error);
alert('Error: ' + error);
}
});
});
</script>
[HttpPost]
public JsonResult GetDataChartTSDT(string tgl1, string tgl2)
{
List<TblTimeSheetDt> data = new List<TblTimeSheetDt>();
List<TblTimeSheetDt> label = _context.TblTimeSheetDts.Select(ts =>ts.KodeUnit).ToList();
var distinctObject = label.Select(ts => ts.KodeUnit).Distinct().ToList();
data.Add(label);
List<TblTimeSheetDt> TotalBayar = _context.TblTimeSheetDts.Where(ts => ts.Tgl >= tgl1 && ts.Tgl <= tgl2).Select(ts => ts.Total).ToList();
data.Add(TotalBayar);
return new JsonResult (data);
}