如何根据下拉列表选择动态显示/隐藏控件(cshtml)

时间:2019-05-11 16:14:39

标签: c# visual-studio asp.net-mvc-4

我有一个下拉列表,我想根据下拉列表中选项的选择显示和隐藏文本框。

到目前为止,我还没有尝试过任何代码,因为我无法在线找到任何专门使用Razor(cshtml)的解决方案,而是看到了其他使用jquery和javascript的解决方案。

但是,如下所示,我的代码不起作用,因为即使我使用了if语句,也会收到null错误。而且即使它确实起作用,我还是担心它们以哪种方式构造我的代码,如果我选择其他选项,文本框也不会消失。

我的视图模型中有一个枚举类,我的下拉列表从中获取选项-这是我的视图模型

        public AccountType UserAccType { get; set; }

    }
        public enum AccountType
        {
            Cheque , Savings , Credit
        }

在我看来,我有从视图模型中的enum类获取选项并将其添加到下拉列表中的代码,并且我拥有一系列if语句,这些语句应该在选项为时显示一个文本框。在下拉列表中选择。

@Html.DropDownList("UserAccType", new SelectList(Enum.GetValues(typeof(AccountType))), "Select Account Type", new { @class = "form-control" })

    @{ if (Model.UserAccType == AccountType.Cheque)
        {
            Html.Label("Cheque", "Savings Amount:");
            Html.TextBox("Cheque" , null , new { @class = "form-control" });
        }
     }

    @{ if (Model.UserAccType == AccountType.Savings)
        {
            Html.Label("Savings", "Interest Amount:");
            Html.TextBox("Savings", null, new { @class = "form-control" });
        }
    }

    @{ if (Model.UserAccType == AccountType.Credit)
        {
            Html.Label("Credit", "Credit Limit:");
            Html.TextBox("Cheque", null, new { @class = "form-control" });
        }
    }

我希望当我在下拉列表中选择一个选项时会出现一个文本框,并且如果更改下拉列表中的选项,我希望该文本框消失而另一个框会重新出现。

0 个答案:

没有答案