Solved

MVC View Foreach Var in Model?

Posted on 2013-01-26
7
2,683 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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