简体   繁体   中英

working with Razor and javascript to get a drop down list element

I am trying to change the text of a div depending on the selected element in the drop down list

this is what I have in my view.

<select name="Department"  id="Department" onchange="mychange(this.value);">
@foreach (string s in Model.DepartmentList)
{
    <option value="@s.ToString()">@s.ToString()</option>
}
</select>
will be assign to:
<div id="testjs" > @Model.assignedtoList.ElementAt(1) </div>

and in my java script

    function mychange(value) {
    var dropDown = document.getElementById("Department");
    var indx = dropDown.selectedIndex;
    document.getElementById("testjs").innerText = "@(Model.assignedtoList.ElementAt(indx))";
    alert(dropDown.selectedIndex);
}

the error is caused at the "@(Model.assignedtoList.ElementAt(indx))"; since the indx variable could not be found. How can I get around it?

The JavaScript code is executed after the server-side code. I donot think you will be able to pass indx from javascript to server side razor code.

Try using Ajax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM