Solved

MVC View Foreach Var in Model?

Posted on 2013-01-26
7
2,538 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

10 Experts available now in Live!

Get 1:1 Help Now