相关文章推荐
考研的手电筒  ·  使用 Markdown ...·  1 年前    · 
幸福的芹菜  ·  JavaScript let 和 ...·  1 年前    · 
体贴的红金鱼  ·  python - plotting ...·  1 年前    · 
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(); /* 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++) { <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.
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
So you need:
@model List<Testing.Models.ModelSearch1>

and:
@for (int index = 0; index < Model.Count; index++)
    <input type="hidden" asp-for="Model[index].Id" />
	
If you want to output a string containing HTML markup, you need to use the Html.Raw method:
@{ string name = "Position 1<sup>st</sup>"; }
@Html.Raw(name)
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •