Click here to Skip to main content
15,878,028 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi There,
i'm having a problem passing a model with array to the HttpPost action.

i have this code:
public class Service
        public string name { get; set; }
        public ServiceProblem[] problems { get; set; } //this is a class array which contains data

//when a user enters a query, it returns the services from DB
//the servicesList contains the class Service with all the relevant data
        public JsonResult AutoCompleteServiceProviders(string query)
            List<Object> services = GetServices();
            List<Service> servicesList = new List<Service>();
            foreach (var item in services)
                servicesList.Add((item as Service));

            var result = servicesList
                .Where(c =>
                .Select(c => new { Value =, Label =, p = c.problems });
            return Json(result, JsonRequestBehavior.AllowGet);

when debugging the Lambda expression i can clearly see the c.problems has the ServiceProblem[]
the js code, manages the data:
                    url: requestUrl,
                    dataType: "json",
                    data: { query: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.Label,
                                value: item.Label,                               
                                realValue: item.Value

but when i get into the
        public ActionResult Index(Service fxService)
            return RedirectToAction("Select", "Create", fxService);

the fxService has two object, one the currect name of the selection form the view
but the "problems" object is null.

also the .cshtml code:
@using (Html.BeginForm("Index", "Create"))
                   <fieldset>                                         @Html.AutocompleteFor(Url.Action("AutoCompleteServiceProviders", "Create"), true, "ex. Shower", c =>, c => c.problems)
                       <input type="submit" id="search" value="" />

kindly your help.
Updated 19-Mar-14 0:04am

1 solution

Doesn't MVC4 have WebAPI ? That's what your AJAX methods should be using. It will JSONify your object automatically, including the collections in it.

I would pass the id of a service on the url, and not expect to be able to pass the entire object. I'd expect to look the object up in the DB. One cool thing about MVC is the control you have over the full URL, and it's better to not have a long URL with gibberish to the human reader on it.
Share this answer
eyalle 23-Mar-14 2:26am    
Hi Christian,
since im kind of new to MVC,
can u apply an example, regarding my code with you suggestion solution ?
that would be great.
Christian Graus 23-Mar-14 18:56pm    
Really, you should read a book on MVC. The example has too many bits of code for me to be able to write it out for you. It's very simple though. Pass an id to the URL, and then look the object up from the DB using the id.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900