Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于使用ASP.NET MVC从级联下拉列表中选择的值填充文本框_C#_Asp.net_Asp.net Mvc_Textbox_Dropdown - Fatal编程技术网

C# 基于使用ASP.NET MVC从级联下拉列表中选择的值填充文本框

C# 基于使用ASP.NET MVC从级联下拉列表中选择的值填充文本框,c#,asp.net,asp.net-mvc,textbox,dropdown,C#,Asp.net,Asp.net Mvc,Textbox,Dropdown,我试着搜索帖子,也没有任何结果,也许我没有使用正确的词 我正在使用ASP.NET MVC创建带有表单的网页 表单有两个下拉列表和一个文本框 第一个dropdownlist由数据库中的值填充 第二个dropdownlist根据所选的第一个dropdownlist值用级联值填充 textbox将根据所选的第二个dropdownlist值,使用同一表格中的值和第二个dropdownlist填充 我尝试从视图中的控制器调用函数,但没有成功。没有错误,但文本框为空 求你了,你能帮我吗 下面是我的代码 查看

我试着搜索帖子,也没有任何结果,也许我没有使用正确的词

我正在使用
ASP.NET MVC
创建带有表单的网页

表单有两个
下拉列表
和一个
文本框

第一个
dropdownlist
由数据库中的值填充

第二个
dropdownlist
根据所选的第一个
dropdownlist
值用级联值填充

textbox
将根据所选的第二个
dropdownlist
值,使用同一表格中的值和第二个
dropdownlist
填充

我尝试从视图中的控制器调用函数,但没有成功。没有错误,但
文本框
为空

求你了,你能帮我吗

下面是我的代码

查看

@Html.DropDownListFor(m => m.StateId, Model.States, "Select", new { @id = "StateId", @Class = "textarea" })
@Html.TextBoxFor(m => m.GPS, new { @Class = "textarea", placeholder = "GPS" })

@section Scripts {

    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/Scripts/DatePicker.js");
    @Styles.Render("~/Content/cssjqryUi")

    <script type="text/javascript">
        $(function () {
            $('[id*=StateId]').on('change', function () {
                var fruitId = $(this).find("option:selected").val();
                if (fruitId != "") {
                    $.ajax({
                        type: "POST",
                        url: "/Home/GetFruitName",
                        data: "id=" + fruitId,
                        success: function (response) {
                            if (response != "") {
                                $('[id*=GPS]').val(response);
                            } else {
                                $('[id*=GPS]').val('');
                            }
                        }
                    });
                } else {
                    $('[id*=GPS]').val('');
                }
            });
        });

        $(function () {
            $("select").each(function () {
                if ($(this).find("option").length <= 1) {
                    $(this).attr("disabled", "disabled");
                }
            });

            $("select").change(function () {
                var value = 0;
                if ($(this).val() != "") {
                    value = $(this).val();
                }
                var id = $(this).attr("id");
                $.ajax({
                    type: "POST",
                    url: "/Home/AjaxMethod",
                    data: '{type: "' + id + '", value: "' + value + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var dropDownId;
                        var list;
                        switch (id) {

                                case "StateId":
                                dropDownId = "#CityId";
                                list = response.Cities;
                                PopulateDropDown("#CityId", list);

                                break;
                        }

                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });

        function DisableDropDown(dropDownId) {
            $(dropDownId).attr("disabled", "disabled");
            $(dropDownId).empty().append('<option selected="selected" value="">Select</option>');
        }

        function PopulateDropDown(dropDownId, list) {
            if (list != null && list.length > 0) {
                $(dropDownId).removeAttr("disabled");
                $.each(list, function () {
                    $(dropDownId).append($("<option>/option>").val(this['Value']).html(this['Text']));
                });
            }
        } 
    </script>
}

您的问题是您从未使用过您型号的
GPS
属性:

success: function (response) {
    var dropDownId;
    var list;
    switch (id) {
        case "StateId":
          dropDownId = "#CityId";
          list = response.Cities;
          PopulateDropDown(dropDownId, list); // You set dropDownId, might as well use it
          $("#GPS").val(response.GPS); // Added to set text in textbox
          break;
    }
}
在你的控制器里

model.GPS = GetGpsById(value); // this is already a string
而不是

model.GPS = GetGps(value).ToString(); // this is the string representation of a jsonified string

您没有使用
GetGps(值)执行任何操作AjaxMethod
中,code>的返回值,我怀疑这是(至少部分)您的问题。毫不迟疑地,你似乎把状态和结果混在一起,渴望得到答复。使用
GetGps(值)
它不应该传递变量来查询和填充文本框?
GetGpsById
返回一个字符串,因此
GetGps
返回一个带有该字符串结果的Json对象,但在
AjaxMethod
中对其不做任何处理。同样在你的javascript中,
success
部分,你调用
PopulateDropDown(“#CityId”,list)
但是
list
似乎没有被设置在任何地方,很抱歉javascript是错误的复制/粘贴,我已经编辑了问题
list=response.Cities。如何解决这个问题?那么,首先在<代码>人物模型中添加<代码>城市>代码>属性,并在返回之前将其填充在<代码> Ajax方法< /C>中(如<代码> Model…Mease=…<代码> >代码>返回JSON(模型); >谢谢,请参见IMG。现在在文本框中我有了
System.Web.Mvc.JsonResult
value是的,这是因为您设置了
model.GPS=GetGps(value)
而不是
GetGps**ById**(value)
太好了,非常感谢您的帮助!
success: function (response) {
    var dropDownId;
    var list;
    switch (id) {
        case "StateId":
          dropDownId = "#CityId";
          list = response.Cities;
          PopulateDropDown(dropDownId, list); // You set dropDownId, might as well use it
          $("#GPS").val(response.GPS); // Added to set text in textbox
          break;
    }
}
model.GPS = GetGpsById(value); // this is already a string
model.GPS = GetGps(value).ToString(); // this is the string representation of a jsonified string