views:

2611

answers:

1

Hi guys,

The premise: Get a dropdownlist's content based on the value selected in the first one. Literal data returns successfully. LINQ queries on a LINQ2SQL Datacontext fail.

Being a total rookie at this MVC/LINQ/jQuery stuff, I battled for HOURS on end trying to figure out what was wrong w/ my understanding of the code. When i hard coded my values, everything worked fine, but as soon as i tried to query a LINQ2SQL datacontext, all sorts of wierd stuff started to happen and, finally, when i duplicated the query results into a new object everything worked again! Forgive my lack of linguistic prowess when it comes to LINQ, but, I think it has to do with the "connectedness" of LINQ2SQL's data. When I create another list representing the queried data, all is well, but if i try to use the queried data itself, jQuery's world falls apart (and without erroring too - which made it hard to figure out).

I would basically like a reference to or an explanation of LINQ2SQL's "connectedness" and how/why it's a problem, especially in these remote/asynchronous calling situations!

See the code below - hope it makes sense & thank you in advance :) Basic Layout:

<script type="text/javascript">

        $(document).ready(function() {
            $("#CaseTypeCategoryId").change(function() {
                $.getJSON("/Cases/CaseNatures", { caseTypeCategoryId: $("#CaseTypeCategoryId option:selected").val() }, function(data) {
                    $("#CaseNatureId option").remove();
                    $("#CaseNatureId").fillSelect(data);
                });
            });
        });

    </script>
            <p>
                <label for="CaseTypeCategoryId">Case Type:</label>
                <%= Html.DropDownList("CaseTypeCategoryId") %>
            </p>
            <p>
                <label for="CaseNatureId">Case Nature</label>
                <select id="CaseNatureId" name="CaseNatureId></select>
            </p>

Controller.aspx

Initially it was just a

SelectList() { new ListItem() { Text = "--Select A Case Nature--", Value = "" }}
and it worked just fine! Then I tried

public JsonResult CaseNatures(int caseTypeCategoryId)
        {
            return this.Json(_caseService.GetCaseNatures(caseTypeCategoryId)
                        .ToList());
        }

This failed - no javascript errors, no compilation issues, couldn't figure it out until i tried dumping the data into a new list manually and i finally settled on:

public JsonResult CaseNatures(int caseTypeCategoryId)
        {
            List returnList = new List();
            returnList.Add(new ListItem() { Text = "--Select A Case Nature--", Value = "" });

            _caseService.GetCaseNatures(caseTypeCategoryId)
                        .ToList()
                        .ForEach(p => returnList.Add(new ListItem() { Value = p.CaseNatureId.ToString(), Text = p.CaseNatureText }));
            return this.Json(returnList);
        }
A: 

Look here - similar problem.

Johannes Setiabudi
Perfect - i stumbled upon a similar solution! Thank you
feemurk