trabajando con Razor y javascript para obtener un elemento de lista desplegable

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?

preguntado el 06 de septiembre de 12 a las 15:09

1 Respuestas

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

Try using Ajax.

Respondido el 06 de Septiembre de 12 a las 16:09

I just noticed that after looking at other issues. I am looking for another approach now by creating an array in js. Thanks - Ammar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.