0

I keep on getting this error and I can't figure out why. This is my code:

 function FindContact(id) {
        var firstNameValue = $("#first-name-" + id).val();
        var lastNameValue = $("#last-name-" + id).val();
        if (firstNameValue && lastNameValue) {
            $("#contact-select-" + id).prop("disabled", false);
            var contacts = [];
            var names = [];
            var companyIds = [];
            @foreach (var contact in Model.Contacts)
            {
                <text>names.push(@contact.FirstName + @contact.LastName + " (DCL)");</text>
                <text>contacts.push(@contact.Id);</text>
                <text>companyIds.push(@contact.CompanyId);</text>
            }
            for (var i = 0; i <= contacts; i++) {
                if (names[i].toLowerCase.indexOf(firstNameValue) >= 0 || names[i].toLowerCase.indexOf(lastNameValue) >= 0) {
                    var option = document.createElement("option");
                    option.value = companyIds[i];
                    option.text = names[i];
                    $("#contact-select-" + id).get(0).add(option, null);
                    $("#contact-select-" + id).trigger("chosen:updated");
                }
            }

      }  
    }

I would really appreciate if someone could help me since I'm very new to programming. Thanks.

BYG
  • 35
  • 7
  • Why is there a razor statement dumping HTML in the middle of your JavaScript? – Cerbrus Aug 30 '17 at 09:55
  • The (rather odd) razor loop is missing quotes delimiting the values in the `push()` call. You can avoid the problem by serialising the `Model.Contacts` list to JSON in the Razor and just outputting that to JS. – Rory McCrossan Aug 30 '17 at 09:55
  • `push` method calls has missing string delimiters (i.e. single/double quotes). I'm curious why you're using `foreach` loop in the middle of a JS code. Use `JsonResult` and AJAX call with `serialize` method to outputting strings into view page. – Tetsuya Yamamoto Aug 30 '17 at 09:57
  • Just serialize your model to a javascript variable using `var contacts = @Html.Raw(Json.Encode(Model.Contacts))` the you can use a simple `.filter()` function to return the value(s) you want –  Aug 30 '17 at 09:59
  • 1
    `for (var i = 0; i <= contacts; i++)` shouldn't this be `contacts.length`? – adiga Aug 30 '17 at 09:59
  • @Stephen Muecke: The problem is that there are over 43000 contact records and when I try to do what you wrote I get this error message: "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property"... That was the problem originally. Thanks. – BYG Aug 30 '17 at 10:17
  • You could handle that as per [this answer](https://stackoverflow.com/questions/15288367/increase-json-response-maxjsonlength-in-mvc-4). But what you doing is crazy - make an ajax call to a server method that returns your value. And there is something definitely wrong with your view is your creating `id` attributes by appending a number. –  Aug 30 '17 at 10:22
  • @BYG you're creating a dropdown with 43000 options?! – adiga Aug 30 '17 at 10:27

0 Answers0