I'm building a feature with a jquery datatable, the idea is to have a list of stores in the parent row, and then when expanding the parent to list all the licensed terminals in child rows that are linked to the store parent row by a StoreLicenseId column. The issue I am having is that I have a ViewModel with two models, one for the list of stores and one for the licensed terminals. I'm busy building the method into my controller, my problem is in the second part of the method where I new up "StoreLicenseDetails = sl.Select(tl => new TerminalListViewModel()", all the references to tl.terminalId and tl.Terminalname. I get this error "StoreListViewModel does not contain a definition for TerminalID and no accessible extension method". I can see why this is happening, so my question really is, how do I include this "second" TerminalListViewModel into my method to form part of the query ?
ViewModel
public partial class StoreListViewModel
{
public List<TerminalListViewModel> StoreLicenseDetails { get; set; } = null!;
public int Id { get; set; }
public Guid StoreLicenseId { get; set; }
[DisplayName("Store Name")]
public string StoreName { get; set; } = null!;
[DisplayName("App One Licenses")]
public int QtyAppOneLicenses { get; set; }
[DisplayName("App Two Licenses")]
public int QtyAppTwoLicenses { get; set; }
[DisplayName("Date Licensed")]
public DateTime DateLicensed { get; set; }
[DisplayName("Licensed Days")]
public int LicenseDays { get; set; }
[DisplayName("Is License Active")]
public bool LicenseIsActive { get; set; }
}
public partial class TerminalListViewModel
{
public int Id { get; set; }
public Guid StoreLicenseId { get; set; }
public Guid TerminalId { get; set; }
public string TerminalName { get; set; } = null!;
public string LicenseType { get; set; } = null!;
public int TerminalLicenseDays { get; set; }
public DateTime DateLicensed { get; set; }
public bool LicenseIsActive { get; set; }
public bool IsDecommissioned { get; set; }
public DateTime LastLicenseCheck { get; set; }
}
Controller Method
//sl = StoreList
//tl = TerminalList
public IEnumerable<StoreListViewModel> GetStoreList()
{
return GetStoreList().GroupBy(sl => new { sl.StoreLicenseId, sl.StoreName, sl.QtyAppOneLicenses,
sl.QtyAppTwoLicenses, sl.DateLicensed, sl.LicenseDays,
sl.LicenseIsActive })
.Select(sl => new StoreListViewModel()
{
StoreName = sl.Key.StoreName,
QtyAppOneLicenses = sl.Key.QtyAppOneLicenses,
QtyAppTwoLicenses = sl.Key.QtyAppTwoLicenses,
DateLicensed = sl.Key.DateLicensed,
LicenseDays = sl.Key.LicenseDays,
LicenseIsActive = sl.Key.LicenseIsActive,
StoreLicenseId = sl.FirstOrDefault().StoreLicenseId,
StoreLicenseDetails = sl.Select(tl => new TerminalListViewModel()
{
StoreLicenseId = tl.StoreLicenseId,
TerminalId = tl.TerminalId,
TerminalName = tl.TerminalName,
}).ToList()
}).ToList();
}
Based on the error,I suppose your GetStoreList() method returns List<OrderListViewModel> ,but your OrderListViewModel doesn't contains properties of TerminalListViewModel,So you got the error
GetStoreList() method should return List<SourceModel>( Source is the model which contains all the properties of StoreListViewModel and TerminalListViewModel)
For example,the link your provided:Multiple child rows in datatable, data from sql server in asp.net core
public class OrderList
{
//source of properties of OrderListViewModel(parent rows)
public int OrderId { get; set; }
public string Customer { get; set; }
public string OrderDate { get; set; }
//source of properties of OrderListDetailViewModel(child rows)
public int KimlikId { get; set; }
public string Product { get; set; }
public string Color { get; set; }
public int Qntty { get; set; }
}
public class OrderListViewModel
{
public int OrderId { get; set; }
public string Customer { get; set; }
public string OrderDate { get; set; }
public List<OrderListDetailViewModel> OrderListDetails { get; set; }
}
public class OrderListDetailViewModel
{
public int KimlikId { get; set; }
public string Product { get; set; }
public string Color { get; set; }
public int Qntty { get; set; }
}
Orderlist contains all columns OrderListViewModel and OrderListDetailViewModel needs.
When it comes to your case,you should
create 3 models (source,parentrow,childrows)
model for parentrows contains the properties
StoreLicenseId,StoreName, QtyAppOneLicenses,QtyAppTwoLicenses, DateLicensed, LicenseDays,LicenseIsActive
and model for childrows contains the other properties of source model
If you still have questions,please show the data you pulled form db,and I'll write a demo for you
Imagine I have two DTOs that share top level types (ServerResponseDTO, ServerCallDetails) but the Items object has different child object (ItemsOfTypeA vs ItemsOfTypeB). What would be the best way to reuse defined top level classes without code duplication? - how can I easily instantiate next objects for ItemsOfTypeC, D and so on.
DTO 1:
public class ServerResponseDTO
{
public int CallId { get; set; }
public ServerCallDetails Details { get; set; }
}
public class ServerCallDetails
{
public int Id { get; set; }
public Items Items { get; set; }
}
public class Items
{
public int Id { get; set; }
public ItemsOfTypeA Items { get; set; }
}
DTO 2:
public class ServerResponseDTO
{
public int CallId { get; set; }
public ServerCallDetails Details { get; set; }
}
public class ServerCallDetails
{
public int Id { get; set; }
public Items Items { get; set; }
}
public class Items
{
public int Id { get; set; }
public ItemsOfTypeB Items { get; set; }
}
but is there a way without passing T to very bottom object?
No. Imagine you could write the following:
public class Items
{
public int Id { get; set; }
public T Items<T> { get; set; }
}
What would be the type of the foo variable in the following code?
var items = new Items();
var foo = pair.Items;
So you have to declare type where your Items<T> is used:
public class ServerResponseDTO<T>
{
public int CallId { get; set; }
public ServerCallDetails<T> Details { get; set; }
}
public class ServerCallDetails<T>
{
public int Id { get; set; }
public Items<T> Items { get; set; }
}
public class Items<T>
{
public int Id { get; set; }
public T FooBar { get; set; }
}
I.e. one of the solution might be to use generic type as Items property so now I can easily create new DTO like: ServerResponseDTO<ItemOfTypeC>
public class ServerResponseDTO<T>
{
public int CallId { get; set; }
public ServerCallDetails<T> Details { get; set; }
}
public class ServerCallDetails<T>
{
public int Id { get; set; }
public Items<T> Items { get; set; }
}
public class Items<T>
{
public int Id { get; set; }
public T Items { get; set; }
}
In my store I have an orders table that captures the orders made by each customer. I want to be able to show the user what quantity of an item they have in their cart. Here is my orders model:
public class Order
{
public int Id { get; set; }
public ApplicationUser Customer { get; set; }
public string ShirtCause {get; set;}
public DateTime CreateDate { get; set; }
public ShirtSize ShirtSize { get; set; }
public ShirtQuantity ShirtQuantity { get; set; }
public decimal Total { get; set; }
public string ShirtYear { get; set; }
public int OrderNum { get; set; }
public bool? OrderCompleted { get; set; }
public bool? RecievedShirt { get; set; }
}
Using code first ShirtQuantity is created in a table and is reflected in the column ShirtQuantity_Id. For reference purposes, here is my quantity model:
public class ShirtQuantity
{
public int Id { get; set; }
public int Name { get; set; }
}
The cart should be output and enumerated in the action with:
return View(db.Orders.ToList());
All except for the Quantity_Id will list when calling
#model IEnumerable<ProjectName.Models.Order>
in the view. How do I get these quantities to list? I have tried creating a view model combining orders and shirtQuantities and calling it in the view:
public class ShirtOrdersViewModel
{
public int ShirtSizeId { get; set; }
public IEnumerable<ShirtSize> ShirtSizes { get; set; }
public int ShirtQuantityId { get; set; }
public IEnumerable<ShirtQuantity> ShirtQuantities { get; set; }
public int ShirtId { get; set; }
public IEnumerable<Shirt> Shirts { get; set; }
public int FileId { get; set; }
public IEnumerable<File> Files { get; set; }
public int OrderId { get; set; }
public IEnumerable<Order> Orders { get; set; }
public decimal Total { get; set; }
}
and this also does not work.
I have a c# class as below
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
and I need another class with following properties
public class EditStudent
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime Date_of_Birth { get; set; }
}
I have repeated properties except that one field (Date_of_Birth) is added in the EditStudent Model.
Is there an option to reuse some of the properties from previous CreateStudent model
I am going to handle these data as Json objects in my front end angularjs based application
You could do this with a null-able property.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime? Date_of_Birth { get; set; }
}
This way you only have one Student model that can accommodate both use-cases.
you should be using inheritance feature here.
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
public class EditStudent : CreateStudent
{
public DateTime Date_of_Birth { get; set; }
}
I've been working on this for a while now and i still haven't come up with a good idea.
I have the fallowing 3 types Project, User and Result. A user and a project can have multiple results. My problem now is that a result can be of multiple types (12 right now and that can change). I have no idea how i'm supposed to structure this the only things that this results have in common is a type and an Id and i want a table for each of the different 12 types.
So what i have until now is this.
Project
public class Project :ITypedEntity<ProjectType>
{
public int Id { get; set; }
public string Name { get; set; }
public string Nationality { get; set; }
public virtual ProjectType Type { get; set; }
public string TitleEn { get; set; }
public string TitleRo { get; set; }
public string Acronym { get; set; }
public string ContractNo { get; set; }
public virtual ActivityField ActivityField {get;set;}
public string SummaryEn { get; set; }
public string SumarryRo { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal Value { get; set; }
public bool IsPartner { get; set; }
public ICollection<User> Team{get;set;}
public string Website {get;set;}
public ICollection<INamedEntity> Results;
}
Result
public class Result:ITypedEntity<ResultType>
{
public int Id { get; set; }
public string Name{get;set;}
public ResultType Type { get; set; }
}
User
public class User:Person
{
public int CNP { get; set; }
public virtual Faculty Faculty { get; set; }
public bool IsUSAMV {get;set;}
public virtual ICollection<INamedEntity> Results {get;set;}
public virtual ICollection<Project> Projects {get;set;}
}
I don't think this helps but i'll paste them anyway
public interface INamedEntity:IEntity
{
string Name{get;set;}
}
public interface ITypedEntity<TType>:INamedEntity{
TType Type { get; set; }
}
public interface IEntity
{
int Id { get; set; }
}
Update 1
Some results types
public class BookChapter:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Book Book {get;set;}
public int PagesInChapter {get;set;}
public string Pagination {get;set;}
}
public class Book:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Person> Authors { get; set; }
public int Year { get; set; }
public Publisher Publisher {get;set;}
public int NumberOfPages { get; set; }
public string Pagination { get; set; }
public int ISBN { get; set; }
}
public class Patent:ITypedEntity<PatentType>
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Authors { get; set; }
public virtual Person Holder{get;set;}
public string Number{get;set;}
public virtual PatentType Type { get; set; }
}
Sorry if the question is not clear, i'll update it with any other information you need.
Thanks.