Reputation: 151
I am always trying to move functionality out of my views and into controllers. Now I have a situation where I have a loop on my page that shows the following data from the view:
<a href=
"@(Model.PageMeta.Url)@string.Format("{0:000}", i)-
@(Model.Power.PowerDetails[i - 1].Q)">
It's messy code for the view and I would prefer to access with a "get" from the viewmodel. One problem is that I would need to pass the value of i back to the viewmodel. Is this possible?
Upvotes: 1
Views: 57
Reputation: 17784
you don't have to use a property when u want to pass a parameter u can use method as suggested by giddy. e.g
public class Power {
public List<PowerDetail> PowerDetails { get; set; }
public string MetaTag { get; set; }
public string GetUrl(int index)
{
return this.MetaTag+index+PowerDetails[index-1].Q
}
}
public class PowerDetail
{
public string Q { get; set; }
}
in View you can do like
for(int i = 0; i<Model.PowerDetails.Count;i++)
{
<a href=
"@(Model.GetUrl(i))">
}
you have to take care of index out of bound situation and string formatting
Upvotes: 2