views:

58

answers:

2

Hi all, I'm trying to keep the values of checkboxes in a form with the same name (level and coursetype) in the querystring so I can check which ones were selected. In the first submit, I get:

Search?coursetype=1416&coursetype=post16&level=3&level=6&level=1

which is fine so I can check the values back into the view and tick the ones previously selected.

However, when I use a pagedList or Html.ActionLink, for example:

Html.ActionLink("search again", "Search", new { coursetype = ViewData["coursetype"], level = ViewData["level"]})

I get: Search?&coursetype=System.String%5B%5D&level=System.String%5B%5D

I tried parsing those values from the array but then when I send them to htmlAttributes in the ActionLink I get: Search?coursetype%3D1416&coursetype%3Dpost16&level%3D3&level%3D6&level%3D1 so the view can't find the values of the checkbox.

Controller:

[AcceptVerbs("GET")]
public ActionResult Search(string[] coursetype, string[] level)
{
   ViewData["level"] = level;
   ViewData["coursetype"] = coursetype;

   return View();
}
A: 

ViewData["..."] is of type object. You want to pass it along as type string[], so you have to make a small change:

Instead of:

Html.ActionLink("search again", "Search", new { coursetype = ViewData["coursetype"], level = ViewData["level"]})

Try:

Html.ActionLink("search again", "Search", new { coursetype = ViewData["coursetype"] as string[], level = ViewData["level"] as string[] })

The only thing I added was as string[] after ViewData["..."].

Hope that helps!

Maxim Zaslavsky
Thanks for your answer Maxim, but I get the same result: a string containing "System.String[]"
tricat
+1  A: 

Are you using a strongly-typed Search view?

I would think you would want to use a ViewModel to pass the data between your view and controller.

public class CourseViewModel
{
    public string Level { get; set; }
    public string CourseType { get; set; }
}

Then your view would be strongly typed to the CourseViewModel so you could construct your ActionLink like this:

Html.ActionLink("search again", "Search", new { coursetype = Model.CourseType, level = Model.Level })

And your controller would look like this:

[AcceptVerbs("GET")]
public ActionResult Search(string coursetype, string level)
{
   var viewModel = new CourseViewModel
   {
       CourseType = coursetype,
       Level = level
   };

   return View(viewModel);
}

Hopefully this helps. I'm not sure if this is what your looking for, but let me know if you have any questions!

Terminal Frost
I recommend this approach, because it's strongly-typed and easy to use. I use custom ViewModels in all of my ASP.NET MVC projects.
Maxim Zaslavsky
I could do that, but not sure is going to solve my problem if the model attribute is a string[]
tricat
Your model is your view model. In my example it would be CourseViewModel. Both properties are strings, it's up to you to set the type of the properties of the view model class. I reread your questions and it appears that you have a bunch of check boxes and not text boxes? If you're using check boxes on a form you'll want to map those to a boolean property on your view model and have optional boolean parameters for each checkbox.
Terminal Frost
Not sure why but doing it this way seems to be working! :)
tricat