首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >基于DropDownList的Asp.Net TextBoxes

基于DropDownList的Asp.Net TextBoxes
EN

Stack Overflow用户
提问于 2018-07-25 03:06:20
回答 1查看 31关注 0票数 0

我有一个DropDownList和3个TextBoxes,如下所示:

代码语言:javascript
复制
@(Html.Kendo().DropDownList()
    .Name("Lager") 
    .DataValueField("LagerId")
    .DataTextField("LagerIdentifier")
    .BindTo((System.Collections.IEnumerable)ViewData["Lager"])
)

@Html.TextBoxFor(model => model.Name1, new { @readonly = "readonly" })
@Html.TextBoxFor(model => model.Name2, new { @readonly = "readonly" })
@Html.TextBoxFor(model => model.Name3, new { @readonly = "readonly" })

基本上,我想要实现的是,如果用户从DropDown中选择了某项内容,我希望相应地自动更改TextBoxes的值。

我的Lager模型如下所示:

代码语言:javascript
复制
public class Lager
{
    public int LagerId { get; set; }
    public string LagerIdentifier { get; set; }
    public string Name1 { get; set; }
    public string Name2 { get; set; }
    public string Name3 { get; set; }
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-25 04:32:55

控制器

代码语言:javascript
复制
public class Names
{
    public string theEnum { get; set; }

    public string Name1 { get; set; }
    public string Name2 { get; set; }
    public string Name3 { get; set; }
}

public enum theEnum
{
    Value1,
    Value2,
    Value3
}

public class PassValue
{
    public string passValue { get; set; }
}

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Tut112(PassValue passVal)
    {
        return Json(new
        {
            RetVal = passVal.passValue
        }
        , @"application/json");
    }

    public ActionResult Tut112()
    {
        return View();
    }

视图

代码语言:javascript
复制
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut112</title>
    <link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#ajaxButton").click(function () {
                var SendData = { passValue: $("#passValue").val() };

                $.ajax({
                    type: "POST",
                    url: '/Home/Tut112',
                    data: SendData,
                    dataType: "json",
                    success: function (data) {
                        if (data.RetVal == 'Value1') {
                            $("#Name1").val(data.RetVal);
                        }
                        else if (data.RetVal == 'Value2') {
                            $("#Name2").val(data.RetVal);
                        }
                        else {
                            $("#Name3").val(data.RetVal);
                        }
                    }
                });
            })
        })
    </script>
</head>
<body>
    @model Testy20161006.Controllers.Names
    @Html.DropDownList("Envs", new SelectList(Enum.GetValues(typeof(Testy20161006.Controllers.theEnum))), "Select Enivronment", new { id = "passValue", @class = "form-control" })
    <input type="button" id="ajaxButton" value="Submit" />
    @Html.TextBoxFor(model => model.Name1, new { @readonly = "readonly" })
    @Html.TextBoxFor(model => model.Name2, new { @readonly = "readonly" })
    @Html.TextBoxFor(model => model.Name3, new { @readonly = "readonly" })
</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51505902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档