Link to home
Start Free TrialLog in
Avatar of pointeman
pointemanFlag for United States of America

asked on

MVC View Foreach Var in Model?

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>
ASKER CERTIFIED SOLUTION
Avatar of Roopesh Reddy
Roopesh Reddy
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pointeman

ASKER

Your example works perfectly. I'm working with a Service Reference. Your example is not working with my Service Reference.
Hi,

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

Check whether it is returning the correct data!

Hope it helps u...
Got it!

[controller]
return View(evals);

[view]
 @foreach (var item in Model)
            {
               <li>@item.Comments</li>                    
            }
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.
Thx...