保存后如何显示所选选项

问题描述 投票:0回答:2

我的编辑页面上有一个下拉列表,用户将在其中通过选择一个值来更新状态,单击保存,然后在数据库中更新新值。但问题是,当我查看它时,下拉列表不会更改。

示例:在数据库中显示“已批准”,但是下拉列表中显示的值为“待处理”(请参见下图)

enter image description here

有人可以帮我显示用户选择的选项,谢谢!

前端

<select class="form-control" id="LV_STATUS" name="LV_STATUS">
    <option>Pending</option>
    <option>Approved</option>
    <option>Denied</option>
</select>

后端

if (ModelState.IsValid)
{
    eMP_LEAVES.LV_STATUS = Request.Form["LV_STATUS"].ToString();
    db.Entry(eMP_LEAVES).State = EntityState.Modified;
    db.SaveChanges();
}
html dropdown
2个回答
1
投票

问题是,您必须设置一个循环,就像我在下面所做的那样,查找数据库状态,这是一个接受数据库值的函数,它将在select标记中循环。

我希望这是有道理的,并且您明白了

谢谢:)

var LV_STATUS = document.getElementById('LV_STATUS')

function indexx(database_Value){
    for(let i=0 ; i<LV_STATUS.options.length ; i++)
    {
        if(database_Value === LV_STATUS.options[i].text)
        {
            LV_STATUS.options[i].selected = true
            return
        }
    }
    }
    
    indexx("Pending") // pass database value here
<select class="form-control" id="LV_STATUS" name="LV_STATUS">
    <option>Pending</option>
    <option>Approved</option>
    <option>Denied</option>
</select>

0
投票

您可以使用selected属性如下显示用户选择的选项

<select class="form-control" id="LV_STATUS" name="LV_STATUS">
    <option (LV_STATUS=='Pending' ? 'selected="selected"' : '')>Pending</option>
    <option (LV_STATUS=='Approved' ? 'selected="selected"')>Approved</option>
    <option (LV_STATUS=='Denied' ? 'selected="selected"')>Denied</option>
</select>

记住:用包含存储在DB中的状态值的变量替换LV_STATUS

© www.soinside.com 2019 - 2024. All rights reserved.