public class ModelSearch { [Key] 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; } }
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*/}); 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++) { <tr> <td> <input type="hidden" asp-for="modelSearch[item].Id" value="@Model.ModelSearch[item].Id"/> </td> </tr> } 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'.
ModelSearch1
Thanks for the response, It didnt work I hard to change @model <Testing.Models.ModelSearch1 to @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
@model List<Testing.Models.ModelSearch1>
@for (int index = 0; index < Model.Count; index++) { <input type="hidden" asp-for="Model[index].Id" /> ... }
Html.Raw
@{ string name = "Position 1<sup>st</sup>"; } @Html.Raw(name)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)