I am learning C# and dotnet core as I want to move away from PHP for my web apps. I am having a specific problem here and I can't get my head around it.
I will show you all the code below, but here is a summary of what I want to achieve: I am getting a table from SQL database. I can display the table in full on my page using foreach loop. However when I am trying to convert the result into an array so I can access a specific item from the returned table I get into all sorts of errors.
By the way I was using this tutorial to supplement my Udemy learning course: https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html
Might be worth mentioning that I was reverse engineering the Models from existing database (as it is in the linked tutorial above)
Here are my files, I'll explain errors below:
Model:
namespace ASPNET_Core_1_0.Models
{
public partial class Graph
{
public int AutoId { get; set; }
public int Numbers { get; set; }
}
}
ModelContext:
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace ASPNET_Core_1_0.Models
{
public partial class GraphsContext : DbContext
{
public virtual DbSet<Graph> Graph { get; set; }
public GraphsContext(DbContextOptions<GraphsContext> options)
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Graph>(entity =>
{
entity.HasKey(e => e.AutoId)
.HasName("PK_Blog");
});
}
}
}
Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ASPNET_Core_1_0.Models;
using Microsoft.EntityFrameworkCore;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace ASPNET_Core_1_0.Controllers
{
public class GraphsController : Controller
{
private GraphsContext _context;
public GraphsController(GraphsContext context)
{
_context = context;
}
public IActionResult Index()
{
return View(_context.Graph.ToArray());
}
public IActionResult GraphJ()
{
var graphs = _context.Graph.FromSql("SELECT * FROM dbo.Graph").ToList();
ViewBag.YourHtml = graphs;
return View(_context.Graph.ToList());
}
}
}
In MS tutorial the Index view used ToList() method:
public IActionResult Index()
{
return View(_context.Graph.ToList());
}
I changed it to ToArray() in hope it will behave like one :/
Index View
@model IEnumerable<ASPNET_Core_1_0.Models.Graph>
@{
ViewBag.Title = "Graphs";
}
<h2>Graphs</h2>
<table class="table">
<tr>
<th>Id</th>
<th>Numbers</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.AutoId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Numbers)
</td>
</tr>
}
</table>
@{
// int[] arr = Model.ToArray();
// int[] array = Model.Cast<int>().ToArray();
// int somenumber = array[5];
}
<div class="row">
<div class="col-md-12 text-center">
@Html.DisplayForModel(Model)
<br />
</div>
</div>
The problem:
If you look at my index view, there are three commented out lines where I was trying to cast the Model object to array so I could access each item in the returned table independently.
Upon running the project I am getting an error:
An unhandled exception occurred while processing the request.
InvalidCastException: Unable to cast object of type 'ASPNET_Core_1_0.Models.Graph' to type 'System.Int32'.
Question: How can I get Model object into an array so I can display specific item from the table instead of the whole table.
Say I want to return Value field for ID 4?
I understand this most likely is a rookie question, but I am having real trouble getting or even understanding the answers I see online.
Table data
My table data is simple two column key -> value pair. ID number and corresponding value.
ID Value
1 10
2 20
3 30
4 40
5 50
ect
Research
Best way to convert IList or IEnumerable to Array
Convert IEnumerable<int> to int[]
Error: "Cannot implicitly convert type"
C# - Cannot implicitly convert type List<Product> to List<IProduct>
https://www.tutorialspoint.com/csharp/csharp_arrays.htm
https://www.dotnetperls.com/array
https://www.dotnetperls.com/list