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
[Key]
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();
var
ve =
new
List<ModelSearch1>();
ve.Add(
new
ModelSearch1 {ModelSearch = ak
});
ViewBag.search = 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
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.