1

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?

2
  • What type of editor you have? TextBox ? Commented Oct 28, 2015 at 6:32
  • Yes, textbox. I've been following tutorials on the asp.net website. Commented Oct 28, 2015 at 6:41

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for the late reply. I can only practice on my free time. Thank you for the answer. It didn't work as well as I thought it would, but I managed to look into other alternative methods.
Sure, I appreciate your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.