Solved

MVC View Foreach Var in Model?

Posted on 2013-01-26
7
2,857 Views
Last Modified: 2013-01-28
Need help understanding this View foreach issue. Get an error within the view foreach:
 
                     "Message='System.Array' does not contain a definition for 'evals'"

I have debugged the controller and verify data is present in -> (return View(evals);)


[HomeController]
 public class HomeController : Controller
    {
        EvalServiceClient client = new EvalServiceClient("WSHttpBinding_IEvalService");        

        public ActionResult Index()
        {
            Eval eval = new Eval();
            eval.Comments = "This came from code!";
            eval.Submitter = "Aaron";
            eval.TimeSubmitted = DateTime.Now;

            client.SubmitEval(eval);

            Eval[] evals = client.GetEvals();

            //foreach (Eval ev in evals)
            //    Console.WriteLine(ev.Comments);

            return View(evals);
        }

        public ActionResult About()
        {
            return View();
        }
    }

[View]

@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC ...</p>

<div>
    <div id="Tags">
           @foreach (var ev in Model.evals)
            {
                @ev.comments
            }
        </div>
</div>
0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 138 total points
ID: 38824155
Hi,

Check this sample  -

HomeController
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            Student[] student = new Student[]{
                new Student{
                        RollNo = 1,
                        Name = "Bill",
                        Branch = "CSE"
                },

                new Student{
                        RollNo = 2,
                        Name = "Steve",
                        Branch = "CSE"
                },
                new Student{
                       RollNo = 3,
                       Name = "Larry",
                       Branch = "CSE" 
                }
            };

            return View(student);
        }

Open in new window


View - Index

<div id="Tags">
    <ul>
        @foreach (var item in Model)
        {
            <li>
                @item.Name
            </li>
        }
    </ul>
</div>

Open in new window


Hope it helps u...
0
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 137 total points
ID: 38824227
In this case the Model doesn't contain a property called evals, the model is the evals. Your foreach should just be:

@foreach( var item in Model )

Open in new window

0
 

Author Comment

by:pointeman
ID: 38827036
Your example works perfectly. I'm working with a Service Reference. Your example is not working with my Service Reference.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38827135
Hi,

Again, we should see what your Service Reference is returning!

Check whether it is returning the correct data!

Hope it helps u...
0
 

Author Comment

by:pointeman
ID: 38827172
Got it!

[controller]
return View(evals);

[view]
 @foreach (var item in Model)
            {
               <li>@item.Comments</li>                    
            }
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 38827181
What error are you getting after making the change to the foreach in your view? I wouldn't think you're getting the same error anymore. Model was an array of Eval, and saying Model.evals was saying, "get me the 'evals' property of the array represented by Model", which is why you got the error saying System.Array doesn't have an "evals" property.

Changing the foreach as both our posts told you to do should at least have resulted in a different error.
0
 

Author Closing Comment

by:pointeman
ID: 38827194
Thx...
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question