1

I am working on a MVC 5 application. I have a method in the controller that accepts 5 parameters, one of which is a integer array. This array would be used to retrieve specific information from the db based on the values contained in it.

The code for the controller method is shown below:

public JsonResult varyByStudyPeriod(int buildingId, int baselineLocationId, int baselineStandardId, int baselinePeriod, int [] altPeriod)
    {
        var data = from r in db.ResidentialDatas
                   where r.BuildingId == buildingId
                   && r.LocationId == baselineLocationId
                   && r.StandardId == baselineStandardId
                   && r.Year == baselinePeriod
                   //&& altPeriod.Contains(r.Year)
                   select new { r.LCC, r.Year };

        return Json(data, JsonRequestBehavior.AllowGet);

    }

Year is simply just integers from 1 to 40 in the db.

However, the commented line is where the Json request fails. Is it possible to guide me in the right direction?

The url for the JSON request is shown below. The format seems to be the issue as the method does not retrieve the information requested.

/ResidentialBuilding/varyByStudyPeriod?buildingId=1&baselineLocationId=1
    &baselineStandardId=1&baselinePeriod=1&**altPeriod=2,3**
2
  • How does it fail? Are you getting an exception? Do you get actual data for altPeriod or is it null? Commented Sep 2, 2014 at 18:54
  • I just updated the url for the Json Request. The format of the altPeriod seems to be the issue when the method attempts to query the database. Commented Sep 2, 2014 at 18:57

1 Answer 1

1

Your query parameter isn't providing altPeriod in a form the default model binder can understand. If you can't change the format the caller is using, you could change the altPeriod parameter type to string and just split it into integers yourself. If you can change the caller, you could change the way it builds the query string so that it looks like this:

?buildingId=1&baselineLocationId=1&baselineStandardId=1&baselinePeriod=1&altPeriod=2&altPeriod=3
                                                                                    ^^^^^^^^^^^^

The duplication of the altPeriod parameter will tell the model binder that all those values belong together and they'll all end up in your array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.