@Html.DropDownListFor 不使用 ViewModel 的带有值和文本的选项

@Html.DropDownListFor Options with value and text without using ViewModels

假设我有一个简单的 table 组,如下图所示,

我想将其填充为下拉菜单,其中选项 value 作为 GroupIDName 作为 text 如此处所示,

<select id="dropdown">
    <option value="1">superadmin</option>
    <option value="2">Support Desk</option>
    <option value="3" selected="selected">SSL I</option>
    <option value="4">SSL II Admin</option>
    <option value="5">SSL II</option>
    <option value="6">Client</option>
</select>

如何在不使用 ViewModel 和任何复杂性的情况下实现这一点?实际上我只想使用 ViewBag 来做到这一点?可能吗?

注意:此问题仅针对idea sharing purposes请不要认为过于宽泛

是的,我知道肯定有可能,

方法一:

在视图中,您可以简单地创建一个 Dictionary<string, string> 和那些 Dictionary 中的数据,就像这里

    public ActionResult Index() {
            var dictionary = new Dictionary<string, string>();
            var allGroup = GetAllGroupNames(); //Query to get all data from table

            foreach(var ag in allGroup)
            {
                dictionary.Add(ag.GroupId.ToString(), ag.Name);

            }

            ViewBag.selectGroupList = dictionary; // Add dictionary to ViewBag

            return View();
      }

那么您的 DropDownListFor 将如下所示,

 @Html.DropDownListFor(model => model.GroupId, new SelectList((Dictionary<string,string>)ViewBag.selectGroupList, "Key", "Value",new { @id="dropdown"}))

方法二:

 ViewBag.selectGroupList = new SelectList(GetAllGroupNames(), "GroupId", "Name");

在视野中

@Html.DropDownListFor(model => model.GroupId, (SelectList)ViewBag.selectGroupList)

结果:

任何比这更好的方法将不胜感激

请按照以下步骤操作....

在 Models 文件夹下创建一个 "AddressModel" class 并为标签字段和下拉列表值创建属性。

型号

public class AddressModel
{
     public AddressModel()
    {
        AvailableCountries = new List<SelectListItem>();
        AvailableStates = new List<SelectListItem>();
    }
    [Display(Name="Country")]
     public int CountryId { get; set; }
     public IList<SelectListItem> AvailableCountries { get; set; }
    [Display(Name = "State")]
     public int StateId { get; set; }
     public IList<SelectListItem> AvailableStates { get; set; }
}

动作

public ActionResult Index()
   {
         AddressModel model = new AddressModel();
        model.AvailableCountries.Add(new SelectListItem 
        { Text = "-Please select-", Value = "Selects items" });
         var countries = _repository.GetAllCountries();
         foreach (var country in countries)
        {
            model.AvailableCountries.Add(new SelectListItem()
            {
                Text = country.Name,
                Value = country.Id.ToString()
            });
        }
         return View(model);
    }

查看

    @Html.LabelFor(model=>model.CountryId)
    @Html.DropDownListFor(model=>model.CountryId, Model.AvailableCountries)

控制器

 public ActionResult Index() 
  {        
    var allGroup = GetAllGroupNames(); //Query to get all data from table
    ViewBag.selectGroupList = allGroup ;
    return View();
  }

查看

@Html.DropDownListFor(model => model.GroupId, new SelectList(ViewBag.selectGroupList, "Value", "Text",new { @id="dropdown"}))

在控制器中

using (Context db = new Context()){
    var tmp = db.group.ToList();
    ViewBag.GroupId = new SelectList(tmp, "GroupId", "Name");
}

在视图中

@Html.DropDownListFor(model => model.GroupID, (SelectList)@ViewBag.GroupId)