第二个ddl没有正确填充

时间:2016-12-24 19:48:51

标签: asp.net-mvc c#-4.0

我更改了视图代码,但仍然没有填写基金ddl。任何人都可以看到问题所在吗?

此代码通过Json调用ClientInvestmentsSince2,报表控制器代码中的该方法完全执行。然后当控制返回到视图时,我不确定会发生什么。代码响应客户端ddl的选择项,并且应该用所选客户端持有的资金填充基金ddl。有些东西是可疑的......在我选择客户端后,警报并非全部触发。 提醒(" here2")但不是提醒(" here3")提醒(" here4& #34;。)

你能帮忙吗?感谢。

****************************这里是查看代码*********** ****************************

init(timeInterval:repeats:block:)


***************************************这里是ReportController代码*** *********

@model StockHoldings.Models.Investments

<head>
    <script src="jquery-3.1.1.min.js"></script>
    @*<script src="http://code.jquery.com/jquery-1.10.1.min.js" type='text/javascript'></script>*@
</head>

@using (Html.BeginForm())
{
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />

    @Html.DropDownList("Client", ViewData["client"] as SelectList, "Select Client", new { id = "Client_ID", style = "width: 150px;" })<br />
    @*{ @Html.DropDownList("Fund", "Select Fund")};*@
        <select id="Fund" name="Fund" , style="width: 150px;"></select><br />
        <div class="form-group">
            @Html.LabelFor(model => model.PurchaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PurchaseDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PurchaseDate, "", new { @class = "text-danger" })
            </div>
        </div>

}

@Scripts.Render("~/bundles/jquery")
<script type="text/jscript">
    $(document).ready(function () {    
        $('#Client_ID').change(function ()
        {
            alert('here');

            $.getJSON('@Url.Action("ClientInvestmentsSince2", "Reports")', { id: $('#Client_ID').val() }, function (data)
            {
                var items = '<option>Select Fund</option>';
                $.each(data, function (i, Fund)
                {
                    items += "<option value='" + Fund.i + "'>" + Fund.val + "</option>";
                })
                .fail(function (jqxhr, textStatus, errorMessage) { alert(errorMessage); });

                //assign the result to the Fund selectlist
                $('#Fund').html(items);
            });
            alert('here2');
        });
        alert('here3');
    });

1 个答案:

答案 0 :(得分:0)

     $(document).ready(function () {
         $('#Client_ID').change(function () {
             alert('here');

             $.getJSON('@Url.Action("ClientInvestmentsSince2", "Reports")', { id: $('#Client_ID').val() }, function (data) {
                 //On client DDL change you  have to fill second drop down list right so after called a method it willl
                 //return json here and you can then play with that json to fill your second DDL
             });
             alert('here2'); // it will fire when Client ddl change
         });
         alert('here3');// it will fire once when there is page load
       });
        alert('here4');//it will never fire as it's not in ready function

请查看我对您的脚本的评论,我已经解决了一些语法错误以及一些有用的注释,以了解此功能的工作原理希望它有所帮助