asp.net mvc 3 - How to get element of List<string> in foreach -
this action
[childactiononly] public partialviewresult archivesidebar() { var currentdate = datetime.now; var list = new list<string>(); (var startdate = new datetime(2013, 5, 1); startdate.month <= currentdate.month; startdate = startdate.addmonths(1)) { list.add(startdate.tostring("mmmm, yyyy")); if (startdate.month == currentdate.month) break; } return partialview("_archivesidebar", list); }
and _archivesidebar
@model list<string> <div class="sidebar"> <h3>archive</h3> @foreach (string archive in model) { <ul> <li> <h3>{ @html.actionlink(archive, "archive", "blog",new{month=archive?,year=archive ?},null) } </h3> </li> </ul> } </div>
in above view want element of list call archive action year , month parameters ,how can year , month ???(where have have put ? )
this archive action
public actionresult archive(int month, int year) { var posts = _blogrepository.getpostbydate(year, month); return view(posts); }
create viewmodel contain month , year
public class blogperiod { public int month {get;set;} public int year {get;set;} // if want pass formatted date view // add public string formatteddate {get;set;} }
then use build list , return view
var list = new list<blogperiod>(); (...) { // rest of code goes here list.add(mew blogperiod { month = startdate.month, year = startdate.year, // want formatted date, add formatteddate = startdate.tostring("mmmm, yyyy") }); } return partialview("_archivesidebar", list);
then change view to
@model ienumerable<blogperiod> @foreach (string archive in model) { // markup goes here @html.actionlink(archive, "archive", "blog", new{month=archive.month, year=archive.year},null) }
Comments
Post a Comment