Asp.net mvc 3 MVC3中的下拉列表
嘿,朋友们,我正在MVC3项目中使用下拉列表。在这里,用户选择一个选项并保存它。当他/她重新访问页面时,我必须将最初保存的值设置为选定值。实际上,我是根据需要使用自定义html助手来完成这项工作的。但我在这方面遇到了问题。我这样做是因为:Asp.net mvc 3 MVC3中的下拉列表,asp.net-mvc-3,Asp.net Mvc 3,嘿,朋友们,我正在MVC3项目中使用下拉列表。在这里,用户选择一个选项并保存它。当他/她重新访问页面时,我必须将最初保存的值设置为选定值。实际上,我是根据需要使用自定义html助手来完成这项工作的。但我在这方面遇到了问题。我这样做是因为: else if (question_type == 7) { EAI.DAL.EaiEntities entities = new DAL.EaiEntities(); Questionnaire
else if (question_type == 7)
{
EAI.DAL.EaiEntities entities = new DAL.EaiEntities();
QuestionnaireRepository repository = new QuestionnaireRepository(entities);
SelectList typesList = repository.PopDdlList(qid);
output.Append(helper.Label(questiontext));
if (answer == "")
{
output.Append(helper.DropDownList("ddl" + question_id, typesList, "-- select type----));
}
else
{
output.Append(helper.DropDownList("ddl" + question_id, typesList, answer));
}
return helper.Raw(output.ToString());
}
实际上上面的代码从数据库中呈现所选的值,但实际上它替换了“-select type-->”。因此,在保存一次后,如果我访问同一页并保存该页,我可以在Formcollection中获得空值
因此,请建议执行此操作的适当方法我通常会在模型中添加一些属性:
int SelectedCategory { get; set; }
IEnumerable<SelectListItem> Categories { get; private set; }
然后在我的剃须刀标记中执行以下操作:
@Html.DropDownListFor(m => m.SelectedCategory, Model.Categories)
这将以标准MVC方式自动连接。希望这有帮助
@Html.DropDownListFor(m => m.SelectedCategory, Model.Categories)