I have an MVC application which has a class file, just like in the sample sites. The class file has 3 properties(?). When I instantiate an instance of the class and assign the properties their values(?) it all works fine. The thing is, two of the properties
are Lists and I need to loop through them at some point. In my page, I have the object available in the ViewData, but I can't seem to be able to access it's columns. I have one more property which isn't a List and it works perfectly, I just need the other
ones to work like it. Any help would be appreciated! Here is the code:
The Class:
using System;
using System.Collections.Generic;
using MyProject.Models;
namespace MyProject.Models
{
public class CreateUpdateViewData
{
public Customer C { get; set; }
public List CAStates { get; set; }
public List USStates { get; set; }
}
}
1) Refactor the CreateUpdateViewData to the following:
public class CreateUpdateViewData
8 {
9 public Customer C {
get; set; }
10 public List<State> CAStates {
get; set; }
11 public List<State> USStates {
get; set; }
12 }
2) The controller code for the UPDATE method should be fine and doesn't need to be changed..
3) Iterate through u're stuff on in the view, like (and i'm assuming the view is Typed to a CreateUpdateViewData class...):
<%
if (this.ViewData != null)
{
foreach(State state in this.ViewData.CAStates)
{
// Render some html .. like
// Response.Write("Name: " + state.Name + ".<br/>");
}
}
%>
:: Never underestimate the predictability of stupidity ::
bulgarian388
Member
22 Points
110 Posts
MVC Help, Looping through a List<>
Mar 18, 2008 09:25 PM|LINK
Hi guys,
I have an MVC application which has a class file, just like in the sample sites. The class file has 3 properties(?). When I instantiate an instance of the class and assign the properties their values(?) it all works fine. The thing is, two of the properties are Lists and I need to loop through them at some point. In my page, I have the object available in the ViewData, but I can't seem to be able to access it's columns. I have one more property which isn't a List and it works perfectly, I just need the other ones to work like it. Any help would be appreciated! Here is the code:
The Class:
using System; using System.Collections.Generic; using MyProject.Models; namespace MyProject.Models { public class CreateUpdateViewData { public Customer C { get; set; } public List CAStates { get; set; } public List USStates { get; set; } } }tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: MVC Help, Looping through a List<>
Mar 19, 2008 12:25 AM|LINK
did you mean List<State> instead of just List?
pure.krome
Member
532 Points
349 Posts
Re: MVC Help, Looping through a List<>
Mar 19, 2008 01:32 AM|LINK
tgdbm is right.
u need to do the following
1) Refactor the CreateUpdateViewData to the following:
public class CreateUpdateViewData
8 {
9 public Customer C { get; set; }
10 public List<State> CAStates { get; set; }
11 public List<State> USStates { get; set; }
12 }
2) The controller code for the UPDATE method should be fine and doesn't need to be changed..
3) Iterate through u're stuff on in the view, like (and i'm assuming the view is Typed to a CreateUpdateViewData class...):
<%
if (this.ViewData != null)
{
foreach(State state in this.ViewData.CAStates)
{
// Render some html .. like
// Response.Write("Name: " + state.Name + ".<br/>");
}
}
%>