MVC - 的SelectList的清除默认选中的价值 [英] MVC - Clear default selected value of SelectList

查看:300
本文介绍了MVC - 的SelectList的清除默认选中的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有获取与数据库中的数据填充一个的SelectList。该数据来自词典< INT,串方式> 并填充的SelectList像下面

I have a SelectList that gets populated with data from the database. The data comes from a Dictionary<int, string> and populates the SelectList like below.

//...

Courses = new SelectList(GetCourses(),"Key","Value");

//...

在视图,它显示了从数据库的下拉列表中选择默认值的第一个记录。我想清除该值并保留下拉空的,所以用户可以手动选择一个值。如果可能的话不使用JavaScript。

On the View, it shows the first record from the database as the default selected value on the dropdown. I'd like to clear this value and leave the dropdown empty so the user can manually select a value. If possible without using javascript.

//I have this on the view
<%= Html.DropDownListFor(model => model.CourseID, Model.Courses) %>

请帮帮忙!

推荐答案

使用这需要一个optionLabel超载

<%= Html.DropDownListFor(model => model.CourseID, Model.Courses, string.Empty) %>

这篇关于MVC - 的SelectList的清除默认选中的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆