在我的参数中,我有“选项”,它们是
-Gestion
-Colegio
-Estado
-Pais
我想在我的DropDownList中加载它尝试这种方式加载它但它不像把它拿来查看,你还有其他方法吗?
控制器:
public ActionResult Index()
{
List<SelectListItem> lst = new List<SelectListItem>();
lst.Add(new SelectListItem() { Text = "Gestion", Value = "1" });
lst.Add(new SelectListItem() { Text = "Colegio", Value = "2" });
lst.Add(new SelectListItem() { Text = "Estado", Value = "3" });
lst.Add(new SelectListItem() { Text = "Pais", Value = "4" });
ViewBag.Opciones = lst;
return View();
}
我的视线:
@model TNT.Controllers.ResumenParam
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Imprimir", "Resumen", new { id = "PDF" }, FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-field">
@Html.DropDownListFor(model => model.Gestion, Enumerable.Range(2000, 20).Select(x => new SelectListItem { Text = x.ToString() }))
@Html.ValidationMessageFor(model => model.Gestion)
</div>
<div class="editor-field">
@Html.DropDownList("Opcion", ViewBag.Opciones)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
模型:
public class CampoResumenColegioParam
{
// [Required(ErrorMessage = "Ingrese una Gestion")]
// [Display(Name = "Gestio")]
// [DataType(DataType.int)]
public int Gestion { get; set; }
public int Opciones { get; set; }
}
尝试将它们保存
SelectListItem
到 aViewBag
然后将数据传递给 helperDropDownList
。在您的操作中,您将拥有以下内容:
在您看来,然后显示它将是这样的:
一种可能性是使用可能的值创建一个枚举:
您的模型的属性可以是枚举的类型:
在视图上,您可以使用 `EnumDropDownListFor' 扩展方法: