Question

I have this code in my view :

    <div class="drop-down-list">
        <%: Html.EditorFor(model => model.StageId,new { id="hiddenStageID"}) %>
        <%: Html.ValidationMessageFor(model => model.StageId) %>
    </div>

And this is my javascript code to set value of EditorFor. But it does not work :

<script>
           var e = document.getElementById("stageTwoView");
           var listIndex = e.selectedIndex;
           var stageIdVal = list[listIndex].valueOf();
           document.getElementById('hiddenStageID').value = stageIdVal;
</script>

for example stageIdVal get this value : "1003". But the last line does not work! Why?

Was it helpful?

Solution

EditorFor doesn't accept htmlAttributes as a parameter (which is what I think you're anticipating); that would be TextBoxFor.

What you're doing is passing additionalViewData to the editor template, so unless you're using that in your EditorTemplate, you won't actually be assigning an ID to the element (and therefore can't retrieve it with JS).

If you wanted to give the text field an ID, either customize your editor template, or use TextBoxFor instead so you can pass it additional attributes:

@Html.TextBoxfor(x => x.StageId, new { id = "hiddenStageID" })

Unless I'm wrong (which I may be because I don't have the template visible in front of me. You may have customized it, in which case I'm just making an inaccurate assumption and I can update my answer).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top