Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/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
Asp.net mvc 4 如何在mvc4中更改dropdownlist所选项目时进行回发_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 如何在mvc4中更改dropdownlist所选项目时进行回发

Asp.net mvc 4 如何在mvc4中更改dropdownlist所选项目时进行回发,asp.net-mvc-4,Asp.net Mvc 4,我的页面上有一个下拉列表。在下拉列表中选择值时,我希望更改标签文本。这是我的密码: @model FND.Models.ViewLender @{ ViewBag.Title = "Change Lender"; } @using (Html.BeginForm()) { @Html.Label("Change Lender : ") @Html.DropDownList("Ddl_Lender", Model.ShowLenderTypes) @Html.

我的页面上有一个下拉列表。在下拉列表中选择值时,我希望更改标签文本。这是我的密码:

@model FND.Models.ViewLender

@{
    ViewBag.Title = "Change Lender";
 }

@using (Html.BeginForm())
{
    @Html.Label("Change Lender : ")
    @Html.DropDownList("Ddl_Lender", Model.ShowLenderTypes)
    @Html.DisplayFor(model => model.Description)
 }

更改dropdownlist中的值时,我希望描述相应地更改。

您可以先将描述放入div中,并为下拉列表提供一个唯一的id:

@model FND.Models.ViewLender

@{
    ViewBag.Title = "Change Lender";
}

@using (Html.BeginForm())
{
    @Html.Label("Change Lender : ")
    @Html.DropDownList("Ddl_Lender", Model.ShowLenderTypes, new { id = "lenderType" })
    <div id="description">
        @Html.DisplayFor(model => model.Description)
    </div>
}

尽管如此,我可能误解了您的问题,此描述必须来自服务器。在这种情况下,您可以使用AJAX查询将返回相应描述的控制器操作。我们需要做的就是将此操作的url作为HTML5 data-*属性提供给下拉列表,以避免在javascript文件中对其进行硬编码:

@Html.DropDownList(
    "Ddl_Lender", 
    Model.ShowLenderTypes, 
    new { 
        id = "lenderType", 
        data_url = Url.Action("GetDescription", "SomeController") 
    }
)
现在在
.change
事件中,我们触发AJAX请求:

$(function() {
    $('#lenderType').change(function() {
        var selectedValue = $(this).val();
        $.ajax({
            url: $(this).data('url'),
            type: 'GET',
            cache: false,
            data: { value: selectedValue },
            success: function(result) {
                $('#description').html(result.description);
            }
        });
    });
});
最后一步当然是让这个控制器动作根据选择的值获取相应的描述:

public ActionResult GetDescription(string value)
{
    // The value variable that will be passed here will represent
    // the selected value of the dropdown list. So we must go ahead 
    // and retrieve the corresponding description here from wherever
    // this information is stored (a database or something)
    string description = GoGetTheDescription(value);

    return Json(new { description = description }, JsonRequestBehavior.AllowGet);
}

你的回答节省了我很多时间
public ActionResult GetDescription(string value)
{
    // The value variable that will be passed here will represent
    // the selected value of the dropdown list. So we must go ahead 
    // and retrieve the corresponding description here from wherever
    // this information is stored (a database or something)
    string description = GoGetTheDescription(value);

    return Json(new { description = description }, JsonRequestBehavior.AllowGet);
}