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 GuptaPosted Aug 2, 2024, 3:54 AM
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:
Controller: Make sure your controller is populating the
Itemscollection appropriately.Partial View: Update your partial view to iterate over the
Itemscollection.Main View: Ensure your main view is including the partial view correctly.
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.