如何在AJAX调用期间附加DDL

时间:2015-04-07 13:31:39

标签: javascript jquery ajax asp.net-mvc

每当调用部分视图时,我都试图改变下拉列表的值。目前AJAX调用检索我需要的信息,但用户必须在保存之前更改局部视图中的位置值,基本上我希望在ajax调用期间更改位置的值,从而节省用户更改时间这个价值是手工的,我已经尝试了但显然不起作用(参见下面的代码),关于我哪里出错的任何想法?

_CameraInfo.cshtml(局部视图)

@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调用

     $(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.append(value);
                $('#Sample').html(response);
            },
            error: function (response) {
                if (response.responseText != "") {
                    alert(response.responseText);
                    alert("Some thing wrong..");
                }
            }
        });
    });
    });
});

顺便说一下,DDL由模型数据填充。

如果您需要任何进一步的信息,请与我们联系:)

1 个答案:

答案 0 :(得分:0)

for ddl value
ddl.val(value);
for ddl text
ddl.text(value);
使用.val().text()而不是.append()