简体   繁体   中英

Setting EditorFor value in razor

I have dropdownlist

    <div class="form-group">
        @Html.LabelFor(model => model.grpid, "grpid", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("grpid", String.Empty)
            @Html.ValidationMessageFor(model => model.grpid)
        </div>
    </div>

and editorfor textbox in a razor view.

    <div class="form-group">
        @Html.LabelFor(model => model.code, new { @class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.EditorFor(model => model.code, new { @id = "codeid" })
            @Html.ValidationMessageFor(model => model.code)
        </div>
    </div>

When user select something from dropdown, i need to put selected data into textbox. I tried with jquery

<script type="text/javascript">
    $(function () {
        $('select#grpid').change(function () {
            var gr = $('#grpid').val()
            $('codeid input').val(gr)

        })
    });
</script>

but nothing happened. When i "alert(gr)" popup shows with right value, but i can't get it into textbox.

can someone help me with this?

Have you checked the selector for the text input is correct? It looks like it should be #codeid because you've said said @id="codeid" & you probably don't need the input part, but that depends on what html your editorfor generates

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