0

I have a C# MVC Model as follows;

public class MyModel
{
    ...
    public IEnumerable<MyModel> allDetails { get; set; }
    public int age { set; get; }
    public string gender { set; get; }
    public int schoolid { set; get; }
    ...
}

Now want to retrieve data from allDetails. How can i do it ?

In the controller;

model.allDetails = MyDetails.getAllDetails(); // Saves all details in the Model

Now how can i retrieve age, gender and school from this model; My approach as follows (but its doesn't work)

model.allDetails.age; // This doesn't work
2
  • What is the code on your view? Are you sure you are passing data into the allDetails from your view? Commented Mar 26, 2013 at 8:46
  • The output is not going to be used in the view. I am using the values of the module to further process inside the controller it self. Commented Mar 26, 2013 at 8:48

2 Answers 2

2

In your code model.allDetails is a list (or smth else that implements IEnumerable), so you have to use foreach or another loop, or just First() to get first value from it: model.allDetails.First().age

Sign up to request clarification or add additional context in comments.

Comments

0

I think you got your desing for class MyModel wrong, You should probably have two classes like:

public class MyClass
{
        ...
        public IEnumerable<MyModel> allDetails { get; set; }
        public int age { set; get; }
        public string gender { set; get; }
        public int schoolid { set; get; }
        ...
}


public class MyModel
{
    ...
    public IEnumerable<MyClass> allDetails { get; set; }
    ...

}

Where your MyModel class will contain an IEnumerable of your other class which contains properties. Later you can fill your model through controller.

In your current MyModel class, you are keeping IEnumerable of the class itself, so each object for that class will have another IEnumerable and so on.

You may also see: C# Coding Conventions (C# Programming Guide) for your properties names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.