I’m building a model class in mvc asp.net and I want to use a DataTable with specific structure.
namespace myproject.Models
{
public class CategoriesModel
{
public DataTable Programs { get; set; }
}
}
That works but I have the problem that is only a simple non constructed DataTable and I would like to know if is possible to construct the datatable in the Model for example this is the DataTable I want to use
DataTable Programs()
{
DataTable dt_programs = new DataTable();
dt_programs.Columns.Add("ID");
return dt_programs;
}
So as you can see the DataTable has already an column so when I use it on the View I want to see that Column.
I don’t know why you want to use a DataTable in this way, If you use a Class for the Programs and within the CategoriesModel have a Generic List of type Program. Like below.
This means that if you want to us DataAnnotations to specify validation rules or which editor template to use for the data type, which is all built into ASP.Net MVC. If you use DataTable you need to recreate all of this functionality as you aren’t utilising the framework
I Hope this helps,