Home Replace text in values from dropdowns in razor statements
Reply: 0

Replace text in values from dropdowns in razor statements

user1665 Published in July 21, 2018, 9:52 am

I have a HTML form with some dropdowns. I need the values of the selected item in the dropdowns to replace tags from another dropdown.

@Html.DropDownListFor(model => model.TrainID, new SelectList(Model.TrainItems, "Value", "Text"), htmlAttributes: new { id = "train" })

@Html.DropDownListFor(model => model.ReasonID, new SelectList(Model.ReasonItems, "Value", "Text"), htmlAttributes: new { id = "reason" })

@Html.DropDownListFor(model => model.MessageID, new SelectList(Model.MessageItems, "Value", "Text"), htmlAttributes: new { id = "message" })

I need the values of the train and the reason selected by the user to replace certain parts of the message item selected.

For example, the message selected would say: "Train # [NUM] has been cancelled due to [REASON]. We apologize for the inconvenience." And then it would dynamically populate the message with the selected values from the first two dropdowns into a textbox on the HTML form: "Train # 123 has been cancelled due to weather. We apologize for the inconvenience.":

@Html.EditorFor(model => model.AnnouncementText, new { htmlAttributes = new { id = "text" } })

I've tried to do this through javascript with no luck:

$(function () {
    var str = document.getElementById("message").innerHTML;
    var res = str.replace("[REASON]", model.ReasonName);
    document.getElementById("text").innerHTML = res;

Clearly, the string.replace method isn't meant for razor statements and I'm wondering if there is a such a method or if I'm going about this the wrong way.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308148 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO