My IQ Is not high enough to figure out jQuery and JSON in MVC Entity Framework

Drop down 1, has 5 choices. This doesn’t matter for this thread.
On .change of this dropdown I want to fire JSON that calls controller function
that returns data from database for the 2nd pulldown.
I already have a ton of harcoded pulldown examples. They all are hardcoded values like State/City, Country/Region
My needs are for dynamic data filled 2nd pulldown: (SELECT rows from DWS_BactiBucket WHERE Type=’R’ AND PWS=’99999′ AND IsTOTColiform = "1")
The only where criteria that is dynamic is the PWS . That is what i am sending it depending on what was entered.

Here is the second pulldown on Add view that should get filled at runtime using jQuery/Json

@Html.DropDownListFor(model => model.OrigSampID, new SelectList(Model.OrigSampID,"OrigSampID","SampDate"))

Here is the jQuery code at bottom of Add View.

//Hook onto the SampType list's onchange event 
$("#SampType").change(function() {

$.getJSON('Home/GetOriginalIDs', {thisPWS: $("#PWS").val() }, function (data) { 
//Clear the Model list 
$("#OrigSampID").empty(); 
//Foreach Model in the list, add a model option from the data returned 
$.each(data, function(index, optionData) { 
$("OrigSampID").append("<option value=" + optionData.OrigSampID + ">" + optionData.SampDate + "</option>"); 
});
}); 
}).change();

Here is the function in the HomeController that should get called and it hits the database with a query using the parameter supplied:

        public JsonResult GetOriginalIDs(string thisPWS)
        {
            
            JsonResult result = new JsonResult();

            var positives = (from c in _db.DWS_BactiBucket.OrderByDescending(x => x.SampDate)
                       where c.PWS == thisPWS && c.SampType == "R" && c.IsTOTPresent == "1"
                       select c).Take(6);

            result.Data = positives.ToList();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
          

        } 

So far all I get is this error:

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items

and it points to the 2nd pulldown:

           @Html.DropDownListFor(model => model.OrigSampID, new SelectList(Model.OrigSampID,"OrigSampID","SampDate"))

I know the LINQ query works to populate a pulldown on form load. 
I just need to catch if 1st pulldown changes. The value doesn’t matter for now.
Once picked from 1st pulldown, the PWS id is sent to the controller function to 
ask the database for rows that are relevant. 

who is 

Challenged

Model.OrigSampID

from 

Challenged

new SelectList(Model.OrigSampID,"OrigSampID","SampDate"))

?

Leave a Reply