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

时间:2014-11-17 08:47:08

标签: c# asp.net-mvc-3

我有一个下拉列表,其代码如下。我试图将所选文本保存到模型而不是选定的值,但我似乎无法在任何地方找到有关如何执行此操作的文档。我是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; }

1 个答案:

答案 0 :(得分:0)

我创建了一个解析文本的方法,并根据它是哪个项

返回一个值