【问题标题】:Default selected value默认选择值
【发布时间】:2021-04-14 10:30:27
【问题描述】:

我想为 DropDownList 设置默认选择值。 SQLString : select * from StatusTypes where StatusDescription ="Queued"

控制器代码:

TaxAmdViewModel taxAmdViewModel = new TaxAmdViewModel();
{
   taxAmdViewModel.statusTypeDDL = (from a in opsdb.StatusTypes select a.StatusDescription).ToList();
}

查看页面代码:

<div class="form-group col-md-4">
<label class="control-label">Action</label>
   @Html.DropDownListFor(model => model.statusTypeDDL, new SelectList(Model.statusTypeDDL, "Description"),  new { @id = "statusAction", @class = "form-control" })
</div>

【问题讨论】:

    标签: asp.net-core model-view-controller


    【解决方案1】:

    您可以在 TaxAmdViewModel 中为所选值定义另一个属性。像这样:

    public class TaxAmdViewModel
    {
        public string statusDescription { get; set; }
        public List<string> statusTypeDDL { get; set; }
    }
    
    public class StatusTypes
    {
        public int Id { get; set; }
        public string StatusDescription { get; set; }
    }
    

    然后在控制器中,给它一个你想要被选中的值。

    TaxAmdViewModel taxAmdViewModel = new TaxAmdViewModel();
    {
        taxAmdViewModel.statusDescription = "Queued";
        taxAmdViewModel.statusTypeDDL = (from a in opsdb.StatusTypes select a.StatusDescription).ToList();
    }
    

    还有观点:

    <div class="form-group col-md-4">
        <label class="control-label">Action</label>
        @Html.DropDownListFor(model => model.statusDescription, new SelectList(Model.statusTypeDDL, "Description"), new { @id = "statusAction", @class = "form-control" })
    </div>
    

    【讨论】:

    • 是的,它成功了。谢谢 - @mj1313
    猜你喜欢
    • 2018-07-24
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2019-06-05
    • 2010-12-10
    相关资源
    最近更新 更多