以下是选择列表项的集合: 我想将此选择项转换为SelectList并将其分配给mvc dropdownlist。
List<SelectListItem> Genderitems = new List<SelectListItem>();
Genderitems.Add(new SelectListItem() { Text = "Female", Value = "Female" });
Genderitems.Add(new SelectListItem() { Text = "Male", Value = "Male" });
Genderitems.Add(new SelectListItem() { Text = "Other Gender", Value = "Other Gender" });
Genderitems.Add(new SelectListItem() { Text = "Unspecified or Unknown", Value = "Unspecified or Unknown" });
答案 0 :(得分:1)
可能你正在寻找这个
List<SelectListItem> Genderitems = new List<SelectListItem>();
Genderitems.Add(new SelectListItem() { Text = "Female", Value = "Female" });
Genderitems.Add(new SelectListItem() { Text = "Male", Value = "Male" });
Genderitems.Add(new SelectListItem() { Text = "Other Gender", Value = "Other Gender" });
Genderitems.Add(new SelectListItem() { Text = "Unspecified or Unknown", Value = "Unspecified or Unknown" });
SelectList sl = new SelectList(Genderitems, "Value", "Text");
ViewBag.GenderList = sl;
在您的视图中 如果你不想严格限制选择模型
@Html.DropDownList("drpGender", ViewBag.GenderList as SelectList, "Select Gender", new { id = "drpGender"})
在其他情况下你可以
@Html.DropDownListFor(x=>x.Gender, ViewBag.GenderList as SelectList, " ", new { id = "drpGender"})
答案 1 :(得分:0)
class ProjectView(sqla.ModelView):
'''
def inaccessible_callback(self, name, **kwargs):
if current_user.is_authenticated:
abort(403)
else:
return redirect(url_for('security.login', next=request.url))
'''
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
else:
return True
@property
def _list_columns(self):
return self.get_list_columns()
@_list_columns.setter
def _list_columns(self,value):
pass
@property
def column_list(self):
if not has_app_context() or current_user.has_role('superuser'):
return ['team', 'project_name', 'approve']
else:
return ['team', 'project_name']
form_edit_rules = column_list
create_template = 'rule_create.html'
edit_template = 'rule_edit.html'
和SelectList
都在实施接口List<SelectListItem>
。用于创建下拉列表的扩展方法如下所示:
IEnumerable<SelectListItem>
正如您所看到的,这两种扩展方法都接受public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList);
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList);
而不是IEnumerable<SelectList>
,因此正如@StephenMuecke所说,您无需将SelectList
转换为List<SelectListItem>
。< / p>