Dear All I would like to ask a question I have a view in which i am creating the records and in same view I have one partial view in which I am showing my listing my create view is working fine and it saves the records as well , but my partial view as it need to show the listing when I iterate through the model listing it says model does not exists , basically I dont have model I have one VIEWMODEL in which I have all models Like
public class NewinformationVm
{
public int id,
public person person{get;set;} //here person itself is a class with properties
public student students{get;set;}//here student itself is a class with having properties
public NewinformationVm()
{
students=new students()//initializing me model which are in view model
person=new person()//initializing my model which are in view model
}
}
my partial view is like this
@model myprojectname.ViewModels.NewinformationVm
when i tried
@foreach(var item in model)
it says model does not exists can someone help me

Aman GuptaRank #193 · 12.4K reputationPosted Aug 1, 2024, 11:54 PM EDT
Hi Mark
It seems like the issue arises from the way you're trying to iterate over the model in your partial view. Since
NewinformationVmcontains individual properties (personandstudent) and not a collection, iterating overNewinformationVmdirectly isn't valid. If you want to display a list of items, your ViewModel needs to contain a collection of items you can iterate over.Here’s a way to structure your ViewModel to support listing and iteration in the partial view:
ViewModel with a Collection:
public class NewInformationVm { public int Id { get; set; } public Person Person { get; set; } // Person model public Student Student { get; set; } // Student model public List Items { get; set; } // Collection of items public NewInformationVm() { Person = new Person(); Student = new Student(); Items = new List(); } } public class Item { public int ItemId { get; set; } public string ItemName { get; set; } // other properties... }Controller: Make sure your controller is populating the
Itemscollection appropriately.public ActionResult Create() { var viewModel = new NewInformationVm { Items = GetItems() // Fetch or initialize your list of items here }; return View(viewModel); } private List GetItems() { // Logic to fetch or create items return new List { new Item { ItemId = 1, ItemName = "Item 1" }, new Item { ItemId = 2, ItemName = "Item 2" } // Add more items... }; }Partial View: Update your partial view to iterate over the
Itemscollection.@model myprojectname.ViewModels.NewInformationVm Item List @if (Model.Items != null && Model.Items.Any()) { @foreach (var item in Model.Items) { @item.ItemName } } else { No items available. }Main View: Ensure your main view is including the partial view correctly.
@model myprojectname.ViewModels.NewInformationVm @Html.Partial("_PartialViewName", Model)By adding a collection property (
Items) to your ViewModel and ensuring the partial view iterates over this collection, you should be able to list the records without encountering the "model does not exist" error.