?
Solved

MVC View Foreach Var in Model?

Posted on 2013-01-26
7
Medium Priority
?
2,938 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 552 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 548 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

741 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