Click here to Skip to main content
15,942,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I want to achieve getting data from database
public class ModelSearch
       public int  Id { get; set; }
       public string RegID { get; set; } = null!;
       public string Gender { get; set; } = null!;
       public string FullName { get; set; } = null!;
       public decimal Average { get; set; }
       public decimal Total { get; set; }
       public bool IsActive { get; set; }

public class ModelSearch1
       public int Id { get; set; }

       [Display(Name = "Name")]
       [Required(ErrorMessage = "Name is Required")]
       public string Name { get; set; } = null!;

       public List<ModelSearch>? ModelSearch { get; set; }

What I have tried:

I have this querry on my controller
<pre> var ak = (from c in cdc.Studentdata
                           select new ModelSearch {

                               RegID = c.RegID,
                               FullName = c.Name,
                                                        }).OrderByDescending(c => c.FullName).ToList();

/*This Querry get the list of all user information. I want my View to display ModelSearch1*/

var ve = new List<ModelSearch1>();
                    ve.Add(new ModelSearch1 {ModelSearch = ak /*ak is the querry from modelsearch*/});

           = ve;
my View Page has this
@model Testing.Models.ModelSearch1
my Table display has this 
@for (int item = 0; item <  Model.ModelSearch.Count(); item++) {
                                    <input type="hidden" asp-for="modelSearch[item].Id" value="@Model.ModelSearch[item].Id"/>

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[Testing.Models.ModelSearch1]', but this ViewDataDictionary instance requires a model item of type 'Testing.Models.ModelSearch1'.

Where Am I getting it wrong.. Thanks for your precious time
Updated 11-Aug-22 3:47am

1 solution

The error is pretty clear: your view declares that its model must be a single instance of the ModelSearch1 class, but your action is trying to pass in a list of ModelSearch1 objects.

Either change the action to pass in the expected model, or change the view to match the model passed by the action.
Share this answer
Difameg Network Nigeria 11-Aug-22 10:24am    
Thanks for the response, It didnt work I hard to change       @model <Testing.Models.ModelSearch1
      @model Ienumerable<Testing.Models.ModelSearch>
but the issue i am having is ... The data will have to be posted back to the controller and i am not able to get  asp-for ='item.Id.  or is there any way i can embed  
<input type="hidden" asp-for="modelSearch[item].Id" value="@Model.ModelSearch[item].Id"/> into the views table?

Thanks for your precious time
Richard Deeming 11-Aug-22 10:26am    
As I said, you need to make up your mind; is your view going to display a single item or a list of items?

At the moment, it's like you've made space on your shelf to store a single apple, and you're now trying to shove an entire orchard into that space. It's not going to end well.
Difameg Network Nigeria 11-Aug-22 10:52am    
The view will display multiple items
Richard Deeming 11-Aug-22 12:02pm    
So you need:
@model List<Testing.Models.ModelSearch1>

@for (int index = 0; index < Model.Count; index++)
    <input type="hidden" asp-for="Model[index].Id" />
Difameg Network Nigeria 12-Aug-22 3:41am    
Thank you very much for your time... It worked for me... is their a way to add css inside a code block like this
@{ string name = "My Name is John Doe and Position 1st"; } it display the line break.. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900