下拉幻灯片表格

时间:2014-09-04 16:34:38

标签: jquery asp.net-mvc html.dropdownlistfor

如何在下拉列表中将其添加到客户,客户是furmulárioprenchido?

<div class="col-lg-12">
                    <div class="form-group">
                        @Html.Label(Resources.Client, htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.DropDownList("ClientId", "Choose Client")
                        @Html.ValidationMessageFor(model => model.Client.clientId, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group">
                        <div class="checkbox">
                            @Html.EditorFor(model => model.Client.active)
                            @Html.ValidationMessageFor(model => model.Client.active, "", new { @class = "text-danger" })
                            @Html.Label(Resources.Active, htmlAttributes: new { @class = "control-label col-md-2" })
                        </div>
                    </div>

脚本

<script>
    $(document).ready(function () {

        $("#ClientId").change(function () {

            $.ajax({
                type: 'POST',
                url: '@Url.Action("SelectClient")',
                dataType: 'json',
                data: { id: $("#ClientId").val() },
                success: function (Client) {

                        $("#ClientId").append('<option value="'
                            + Client.name + '"></option>');
                        $("#ClientNif").append('<option value="'
                            + Client.nif + '"></option>');
                },
                error: function (ex) {

                }
            });
            return false;
        })


    });
</script>

public JsonResult SelectClient(int?id)         {             Client Client = new Client();

        if (id.HasValue)
        {
            db.Configuration.ProxyCreationEnabled = false;
            Client = db.Clients.Find(id);
        }
        return Json(Client);
    }

应该点击下拉列表并查看包含客户端所有数据的表单,但单击下拉表单时数据不会出现。

1 个答案:

答案 0 :(得分:0)

您正在尝试对选择html控件的名称执行jquery选择器操作。因此, $(&#34;#ClientId&#34;)在您的上下文中无效。

要修复,请尝试修改以下代码

                    @Html.DropDownList("ClientId", "Choose Client")

@Html.DropDownList("ClientId",Enumerable.Empty<SelectListItem>(),"Choose Client")

此外,使用点击事件替换 onchange 事件。

$(".form-group select[name='ClientId']").on("click", function () {
        // Populate Data
                            });