我有一个表将一个True / False值存储为sqlserver位字段(Featured
)。首先,有没有比手动更好的方法为此生成下拉列表?
其次,这是我迄今为止所做的工作但不会将selected="selected"
添加到DDL中的任何项目。
编辑1:我已根据建议的答案更新了此示例,并导致运行时错误:( 编辑2:像功能一样使用它(使用和不使用强制转换)但仍然没有添加所选=“已选择”。
型号:
//
// View models
public class SpotlightFormViewModel
{
// props
public Spotlight Spotlight { get; private set; }
public SelectList FeaturedMenu { get; private set; }
static IDictionary<string, int> feature = new Dictionary<string, int>(){
{"True", 1},
{"False", 0},
};
public IEnumerable<SelectListItem> FeaturedChoices(Spotlight spotlight)
{
return feature.Select(f => new SelectListItem
{
Text = f.Key,
Value = f.Value.ToString(),
Selected = spotlight.Featured,
});
}
// constr
public SpotlightFormViewModel(Spotlight spotlight)
{
Spotlight = spotlight;
FeaturedMenu = new SelectList(FeaturedChoices(spotlight));
}
}
控制器:
public ActionResult Edit(int id)
{
Spotlight spotlight = spotlightRepository.GetSpotlight(id);
return View(new SpotlightFormViewModel(spotlight));
}
查看:
<div class="editor-label">
<%: Html.LabelFor(model => model.Spotlight.Featured) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.FeaturedMenu, Model.FeaturedChoices(Model.Spotlight))%>
<%: Html.ValidationMessageFor(model => model.Spotlight.Featured) %>
</div>
答案 0 :(得分:3)
我使用与您相同的dropdowlist。但在你的情况下你可以简单地使用一些单选按钮:
<%: Html.Label("True") %>
<%: Html.RadioButtonFor(model => model.FeaturedMenu, "1") %>
<%: Html.Label("False") %>
<%: Html.RadioButtonFor(model => model.FeaturedMenu, "0") %>
其次,您谈到了“选定”值,但您没有在“公共IEnumerable FeaturedChoices”中使用它,您可以像这样添加所选值:
return feature.Select(f => new SelectListItem
{
Text = f.Key,
Value = f.Value.ToString(),
Selected = false
});
答案 1 :(得分:1)
我终于有了这个工作。即使我的语法&amp;当我将变量传递给public IEnumerable FeaturedChoices(Spotlight spotlight)
时,逻辑是正确的,即使我明确标记Selected = true
,它也无法工作。如果有人能回答为什么会这样,我会很感激
正确应用selected="selected"
的唯一方法是,如果我使用getter创建了FeaturedChoices
属性。完整的工作代码
查看型号:
//
// View models
public class SpotlightFormViewModel
{
// props
public Spotlight Spotlight { get; private set; }
public SelectList FeaturedMenu { get; private set; }
static IDictionary<string, int> feature = new Dictionary<string, int>(){
{"True", 1},
{"False", 0},
};
public IEnumerable<SelectListItem> FeaturedChoices
{
get
{
return feature.Select(f => new SelectListItem
{
Text = f.Key,
Value = f.Value.ToString(),
Selected = IsSelected(this.Spotlight.Featured, f.Key)
});
}
}
private bool IsSelected(bool featured, string val)
{
bool result = false;
if (String.Compare(val, "True") == 0 && featured)
result = true;
else if (String.Compare(val, "False") == 0 && !featured)
result = true;
return result;
}
// constr
public SpotlightFormViewModel(Spotlight spotlight)
{
Spotlight = spotlight;
}
}
<强>控制器:强>
public ActionResult Edit(int id)
{
Spotlight spotlight = spotlightRepository.GetSpotlight(id);
return View(new SpotlightFormViewModel(spotlight));
}
查看:强>
<div class="editor-label">
<%: Html.LabelFor(model => model.Spotlight.Featured) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.FeaturedMenu, Model.FeaturedChoices)%>
<%: Html.ValidationMessageFor(model => model.Spotlight.Featured) %>
</div>