I was wondering if there was any way to add text to a EditorFor by selecting something from a DropDownList.
For example, if I have something like this:
@Html.DropDownList("stackoverflow", "--Select Stack--")
@Html.EditorFor(model => model.Stack, new { htmlAttributes = new { @class = "form-control" } })
My question is how would I go about replacing the text in the EditorFor with whatever I select from the DropDownList. If I was using javascript, how would I go about it? Or is there a better way to go about it?
You can try this with jQuery:
@Html.DropDownList("stackoverflow", 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>
$("#stackoverflow").change(function () {
var selectedOptionId = $(this).val();
$("#Stack").val(selectedOptionId); //Set the selected value to textbox
});
</script>