Solved

MVC View Foreach Var in Model?

Posted on 2013-01-26
7
2,592 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now