让 DropDownListFor() 将文本保存到模型而不是值

本文关键字:模型 保存 DropDownListFor 文本 | 更新日期: 2023-09-27 18:30:34

我有一个下拉列表,它的代码如下。我正在尝试将所选文本而不是所选值保存到模型中,但我似乎无法在任何地方找到有关如何执行此操作的文档。我是 mvc 的新手,通过这个学习曲线对我来说是令人沮丧的。感谢您的任何帮助!

查看代码

<div class="row">
    @Html.LabelFor(model => model.Urgency, "Urgency:")
    @Html.DropDownListFor(model => model.Urgency, (SelectList)ViewBag.urgency, "--Select--", new { id = "urgencyList" })
</div>

控制器代码

// set the urgency list
        List<SelectListItem> urgency = new List<SelectListItem>();
        urgency.Add(new SelectListItem() { Text = "6 Hours", Value = "42.9", Selected = false });
        urgency.Add(new SelectListItem() { Text = "12 Hours", Value = "39", Selected = false });
        urgency.Add(new SelectListItem() { Text = "24 Hours", Value = "33.8", Selected = false });
        urgency.Add(new SelectListItem() { Text = "48 Hours", Value = "28.6", Selected = false });
        urgency.Add(new SelectListItem() { Text = "3 Days", Value = "24.7", Selected = false });
        urgency.Add(new SelectListItem() { Text = "4 Days", Value = "22.75", Selected = false });
        urgency.Add(new SelectListItem() { Text = "5 Days", Value = "21.45", Selected = false });
        urgency.Add(new SelectListItem() { Text = "6 Days", Value = "19.95", Selected = false });
        urgency.Add(new SelectListItem() { Text = "7 Days", Value = "18.2", Selected = false });
        urgency.Add(new SelectListItem() { Text = "10 Days", Value = "14.95", Selected = false });
        urgency.Add(new SelectListItem() { Text = "20 Days", Value = "14.95", Selected = false });
        urgency.Add(new SelectListItem() { Text = "30 Days", Value = "14.95", Selected = false });
        ViewBag.Urgency = new SelectList(urgency, "Value", "Text");

型号代码

[Required(ErrorMessage = "Urgency is Required.")]
    public string Urgency { get; set; }

让 DropDownListFor() 将文本保存到模型而不是值

我创建了一个方法来解析文本并根据它所在的项目返回一个值