0

I'm working on an intranet, specifically on a Weekly Monitoring tool that lets me see the Activity of my employees week by week. I'm working on the week by week filter. So far I have figured out how to create a List containing every week of 2015 in the following format :

From dd/mm/yyyy to dd/mm/yyyy

Like such :

public ActionResult WeeklyMonitoring()
{  
    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year); // Creates the list
    ViewBag.Weeks = wl;
    wl.Select(m => new SelectListItem { Text = m, Value = m });           
    return View();
}

But here's the issue, when I select a week in my DropDownList (Where I had to convert it to the right type) :

  <td><%: Html.DropDownList("Weeks", ((List<string>)ViewBag.Weeks).Select(m => new SelectListItem { Text = m, Value = m }), new { onchange = "this.form.submit();"}) %></td>

And it sends a form to :

[HttpPost]
public ActionResult WeeklyMonitoring(FormCollection form)
{
    string week = form["Weeks"];            

    ViewBag.Weeks = week;
    return View();
}

I get the obvious following error :

Cannot convert type 'string' to 'System.Collections.Generic.List'

How may I avoid this error, and keep the selected week in my DropDownList after the form is sent?

EDIT :

So far I have found this solution, probably isn't practical for a filter but here I go :

[HttpPost]
public ActionResult WeeklyMonitoring(FormCollection form)
{
    string week = form["Weeks"];           

    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year);
    wl.Remove(week);
    wl.Insert(0, week);
    wl.Select(m => new SelectListItem { Text = m, Value = m });
    ViewBag.Weeks = wl;
    return View();
}

I remove the item from the list and put it on top, so it gets displayed by the DDList.

EDIT2 :

Adding FetchWeeks :

  public List<string> FetchWeeks(int year)
    {
        List<string> weeks = new List<string>();
        DateTime startDate = new DateTime(year, 1, 1);
        startDate = startDate.AddDays(1 - (int)startDate.DayOfWeek);
        DateTime endDate = startDate.AddDays(6);
        while (startDate.Year < 1 + year)
        {
            weeks.Add(string.Format("From {0:dd/MM/yyyy} to {1:dd/MM/yyyy}", startDate, endDate));
            startDate = startDate.AddDays(7);
            endDate = endDate.AddDays(7);
        }
        return weeks;
    }

EDIT3 : Thanks to kikyalex we have found a solution :

When the ViewBag property has the same name as the DDList that creates a problem that stops it from changing its value.

1 Answer 1

1
 string week = form["Weeks"];            

 ViewBag.Weeks = week;

You get the current week from your dropdown and pass it into view again. Then you cast it as List<string>. It's normal to get an error.

You should use the Selected property from SelectListItem

public ActionResult WeeklyMonitoring()
{  
    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year); // Creates the list
    ViewBag.Weeks =  wl.Select(m => new SelectListItem { Text = m, Value = m }).ToList();
    return View();
}

[HttpPost]
public ActionResult WeeklyMonitoring(FormCollection form)
{
    string week = form["Weeks"];           

    ProjectResourceManagerService pr = new ProjectResourceManagerService(new ModelStateWrapper(this.ModelState));

    List<string> wl = pr.FetchWeeks(DateTime.Now.Year);
    ViewBag.Weeks =   wl.Select(m => new SelectListItem { Text = m, Value = m, Selected = m == week }).ToList();
    return View();
}

In you view change the cast to List<SelectListItem> instead of List<string>

 <td><%: Html.DropDownList("Weeks", ((List<SelectListItem>)ViewBag.Weeks), new { onchange = "this.form.submit();"}) %></td>
8
  • I still seem to get Cannot convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<System.Web.Mvc.SelectListItem> after checking and changing it as you said. Oct 13, 2015 at 13:46
  • 1
    You are right. I forgot to change the GET method too. Updated my answer Oct 13, 2015 at 13:50
  • No more errors ! But it still brings me back to the previous date. (Doesn't change to the selected one). Does ViewBag.Weeks not contain the selected one by now? EDIT : I now Weeks contains the list of weeks, but how come the form doesn't let the selected week in the DDList box? Oct 13, 2015 at 13:53
  • ViewBag.Weeks should contain each week as a SelectListItem object, and only one of them should have Selected == true. You should debug your code and see if week variable contains what you selected from dropdown Oct 13, 2015 at 13:56
  • 1
    According to the accepted answer from here, there is a problem if the Viewbag property has the same name as the dropdownlist Oct 13, 2015 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.