从 DropDownList 向 EditorFor 添加文本
Adding text to EditorFor from DropDownList
我想知道是否有任何方法可以通过 select 从 DropDownList 中输入一些内容来向 EditorFor 添加文本。
例如,如果我有这样的东西:
@Html.DropDownList("Whosebug", "--Select Stack--")
@Html.EditorFor(model => model.Stack, new { htmlAttributes = new { @class = "form-control" } })
我的问题是如何将 EditorFor 中的文本替换为我 select 来自 DropDownList 的文本。如果我使用 javascript,我会怎么做?或者有更好的方法吗?
您可以尝试使用 jQuery:
@Html.DropDownList("Whosebug", Model.StackTypes.Select(stackType => new SelectListItem { Text = stackType, Value = stackType }), "--Select Stack--")
<!-- Just for demo purpose assume, Model.StackTypes above is a List<string>-->
@Html.EditorFor(model => model.Stack, new { htmlAttributes = new { @class = "form-control" } })
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$("#Whosebug").change(function () {
var selectedOptionId = $(this).val();
$("#Stack").val(selectedOptionId); //Set the selected value to textbox
});
</script>
我想知道是否有任何方法可以通过 select 从 DropDownList 中输入一些内容来向 EditorFor 添加文本。
例如,如果我有这样的东西:
@Html.DropDownList("Whosebug", "--Select Stack--")
@Html.EditorFor(model => model.Stack, new { htmlAttributes = new { @class = "form-control" } })
我的问题是如何将 EditorFor 中的文本替换为我 select 来自 DropDownList 的文本。如果我使用 javascript,我会怎么做?或者有更好的方法吗?
您可以尝试使用 jQuery:
@Html.DropDownList("Whosebug", Model.StackTypes.Select(stackType => new SelectListItem { Text = stackType, Value = stackType }), "--Select Stack--")
<!-- Just for demo purpose assume, Model.StackTypes above is a List<string>-->
@Html.EditorFor(model => model.Stack, new { htmlAttributes = new { @class = "form-control" } })
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$("#Whosebug").change(function () {
var selectedOptionId = $(this).val();
$("#Stack").val(selectedOptionId); //Set the selected value to textbox
});
</script>