Buenas tardes, estoy desarrollando una aplicación web basada en MVC que muestra los datos de una tabla sql server y los va actualizando. Recibo bien los datos de sql server mediente el método "GetData()" que retorna una lista con ellos. El problema es que al hacer la petición get al controlador que invoca esta función para retornarlos en la vista, estos datos aparecen como "undefined". He mirado la variable "data" que correspondería con la respuesta de la petición y esta en lugar de contener los datos contiene el tipo de dato que debería de contener, es decir "System.Collections.Generic.List'1".
Función javascript que genera la tabla en la pagina html:
function getData() {
var $tbl = $('#tableEvents');
$.ajax({
url: '../home/Get',
type: 'GET',
datatype: 'json',
success: function (data) {
debugger;
alert(data);
if (data.length > 0) {
var newdata = data;
$tbl.empty();
$tbl.append(' <tr class="columns"><th>@Html.DisplayNameFor(model => model.NameEvent)</th><th>@Html.DisplayNameFor(model => model.Protocol)</th><th>@Html.DisplayNameFor(model => model.IP)</th><th>@Html.DisplayNameFor(model => model.Port)</th><th>@Html.DisplayNameFor(model => model.AccountID)</th><th>@Html.DisplayNameFor(model => model.SessionID)</th><th>@Html.DisplayNameFor(model => model.DateEvent)</th><th>@Html.DisplayNameFor(model => model.SecurityImpact)</th><th>@Html.DisplayNameFor(model => model.Resolved)</th></tr>');
var rows = [];
for (var i = 0; i < newdata.length; i++) {
rows.push(' <tr class="rowEvent"><td>' + newdata[i].NameEvent +
'</td><td>' + newdata[i].Protocol +
'</td><td>' + newdata[i].IP +
'</td><td>' + newdata[i].Port +
'</td><td>' + newdata[i].AccountID +
'</td><td>' + newdata[i].SessionID +
'</td><td>' + newdata[i].DateEvent +
'</td><td>' + newdata[i].SecurityImpact +
'</td><td><input type="checkbox" id="' + newdata[i].Id + '" value="' + newdata[i].Resolved + '"/></td><td><a class="btn btn-xs btn-warning"><span class="glyphicon glyphicon-info-sign"></span></a></td></tr > ');
}
$tbl.append(rows.join(''));
}
}
});
}
HomeController:
SECURITYEVENTSRepository objRepo = new SECURITYEVENTSRepository();
[Route("Home/Get")]
public IEnumerable<SECURITYEVENTS> Get()
{
var events = objRepo.GetData();
return events;
}
Repositorio:
public class SECURITYEVENTSRepository
{
public IEnumerable<SECURITYEVENTS> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [Id],[NameEvent],[Protocol],[IP],[Port],[AccountID],[SessionID],[DateEvent],[SecurityImpact],[Resolved] FROM [events].[dbo].[SECURITYEVENTS]", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var events = new List<SECURITYEVENTS>();
using (var reader = command.ExecuteReader())
events = reader.Cast<IDataRecord>()
.Select(x => new SECURITYEVENTS()
{
Id = (int)x["Id"],
NameEvent = (string)x["NameEvent"],
Protocol = (string)x["Protocol"],
IP = (string)x["IP"],
Port = (int)x["Port"],
AccountID = (string)x["AccountID"],
SessionID = (string)x["SessionID"],
DateEvent = (string)x["DateEvent"],
SecurityImpact = (string)x["SecurityImpact"],
Resolved = (string)x["Resolved"],
}).ToList();
return events;
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
EventsHub.Show();
}
}
Modelo:
public partial class SECURITYEVENTS
{
public int Id { get; set; }
public string NameEvent { get; set; }
public string Protocol { get; set; }
public string IP { get; set; }
public Nullable<int> Port { get; set; }
public string AccountID { get; set; }
public string SessionID { get; set; }
public string DateEvent { get; set; }
public string SecurityImpact { get; set; }
public string Resolved { get; set; }
}
Agradezco vuestra ayuda, Un saludo.