pointeman
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("WSHttpB inding_IEv alService" );
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.Comme nts);
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>
"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("WSHttpB
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.Comme
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Again, we should see what your Service Reference is returning!
Check whether it is returning the correct data!
Hope it helps u...
Again, we should see what your Service Reference is returning!
Check whether it is returning the correct data!
Hope it helps u...
ASKER
Got it!
[controller]
return View(evals);
[view]
@foreach (var item in Model)
{
<li>@item.Comments</li>
}
[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.
Changing the foreach as both our posts told you to do should at least have resulted in a different error.
ASKER
Thx...
ASKER