0

i want the value of location to be changed during the ajax call, thus saving the user time on altering the value by hand, i've attempted it but clearly doesn't work (see the code below), any ideas on where im going wrong?

_CameraInfo.cshtml (partial view)

@model JobTracker.Models.Job


<h2>Edit and Confirm</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Job</legend>

    @Html.HiddenFor(model => model.JobID)

    @Html.HiddenFor(model => model.OrderID)

  <div class="editor-label">
        @Html.LabelFor(model => model.LocationID, "Location")
    </div>
    <div class="editor-field">
        @Html.DropDownList("LocationID", null, new {id ="Location" })
        @Html.ValidationMessageFor(model => model.LocationID)
    </div><br />

    <div class="editor-label">
        @Html.LabelFor(model => model.HighPriority)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.HighPriority, new SelectList(
    new[] 
    { 
        new { Value = "Yes", Text = "Yes" },
        new { Value = "No", Text = "No" },
    },
     "Value",
     "Text",
    Model
))

        @Html.ValidationMessageFor(model => model.HighPriority)
    </div><br />

    <div class="editor-label">
        @Html.LabelFor(model => model.Comments)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Comments)
        @Html.ValidationMessageFor(model => model.Comments)
    </div><br />

      <div class="editor-label">
        @Html.LabelFor(model => model.Status)
    </div>
    <div class="editor-field">
           @Html.DropDownListFor(model => model.Status, new SelectList(
    new[] 
    { 
        new { Value = "In Progress", Text = "In Progress" },
        new { Value = "Completed", Text = "Completed" },
        new { Value = "Not Started", Text = "Not Started" },
        new { Value = "Stopped", Text = "Stopped" },
    },
     "Value",
     "Text",
    Model
))
        @Html.ValidationMessageFor(model => model.Status)
    </div><br />

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

}

AJAX call

     $(document).ready(function () {
    $('#qr-number').on('change', function () {
        var ddl = $('#Location');
        var value = "cleanroom";
        $.ajax({
            type: "Get",
            url: '/CameraInfo/Edit',
            data: { ID: $('#qr-number').val() },
            success: function (response) {
                ddl.val(value);
                $('#Sample').html(response);
            },
            error: function (response) {
                if (response.responseText != "") {
                    alert(response.responseText);
                    alert("Some thing wrong..");
                }
            }
        });
    });
    });
});

The DDL is populated by the model data by the way.

I presume it doesnt alter during the success for some reason, maybe it because the view isnt fully loaded?

Please let me know if you require any further information :)

Matchbox2093
  • 986
  • 2
  • 12
  • 40
  • 1
    couple things to check. make sure the id on the rendered drop down is Location. Some of the helpers will override that change. Next make sure that you have an option in your dropdown that has value="cleanroom". if you are trying to set by text see here http://stackoverflow.com/questions/1888931/set-dropdown-value-by-text-using-jquery – Matt Bodily Apr 08 '15 at 20:13
  • Brillant @MattBodily thanks that makes it a bit cleaner! PS also found out there i needed to make sure that the html response came first then added in the other options upon success, hence my thoughts were right enough in that the html wasnt there for me to change :) thanks again – Matchbox2093 Apr 08 '15 at 20:18
  • Ps please make it into an answer to confirm it as my answer – Matchbox2093 Apr 08 '15 at 20:19
  • posted my comment into an answer. glad it helped :) – Matt Bodily Apr 08 '15 at 20:33

1 Answers1

1

couple things to check. make sure the id on the rendered drop down is Location. Some of the helpers will override that change. Next make sure that you have an option in your dropdown that has value="cleanroom". if you are trying to set by text see Here

Community
  • 1
  • 1
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48