【问题标题】:Generating a selected attribute with DropDownListFor (not DropDownList), by using strongly type model with SelectList通过使用带有 SelectList 的强类型模型,使用 DropDownListFor(不是 DropDownList)生成选定属性
【发布时间】:2015-02-13 11:38:02
【问题描述】:

我尝试使用从控制器传递的 viewModel 填充下拉列表,其主要目标是设置 selected 属性标记,以便当 dropDown-list 加载下拉列表中的特定项目时-列表被选中。我正在使用带有 Razor 的 MVC 5。

有很多与 DropDownListFor 相关的问题,但不幸的是我还没有找到我要找的东西。

  1. 我找到了许多适用于 viewbag 的解决方案,但我不是 我在找什么,我想通过一个填充下拉列表 强类型模型。
  2. 我要使用的 Html 助手是 @Html.DropDownListFor(不是 @Html.DropDownList)。据我所知,带有 For 后缀的 helpers 被 linq 表达式重载,它们用于处理模型并且是强类型的,而非后缀 For 用于 viewbags。

这是应该的样子,默认选择的城市应该是“蒙扎”

目前我已经设法仅使用@Html.DropDownList 来实现此结果,如前所述,这不是我的意图,我想使用@Html.DropDownListFor 来实现相同的结果。为了记录,这是代码:

这种方法使用@Html.DropDownList

型号

public class City
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CityId { get; set; }
    public int ProvinceId { get; set; }
    [Display(Name = "Città")]
    public string Name { get; set; }
    [Display(Name = "CAP")]
    public string ZipCode { get; set; }
    public int Dispose { get; set; }

    public virtual Province Province { get; set; }
}

查看模型

public class AccountIndexGetVM
{
    public UserData userData = new UserData();
    public AddressUser addressUser = new AddressUser();
    public IEnumerable<SelectListItem> cities { get; set; } //<--Cities to DDL
}

控制器操作

//Populating data to the view model and send to view            
accountIndexGetVM.userData = userData;
accountIndexGetVM.addressUser = addressUser;
accountIndexGetVM.cities = new SelectList(db.Cities, "CityId", "Name", addressUser.City.CityId.ToString()); 
return View(accountIndexGetVM);

查看 在

中创建下拉列表
@using MyProject.ViewModels.Account
@model AccountIndexGetVM
//some code...
<td>@Html.LabelFor(m => m.addressUser.City.Name)</td>
<td>@Html.DropDownList("CityAttributeIDVAlue", Model.cities, "Please Select a City")</td>

HTML 源代码结果

<td><select id="CityAttributeIDVAlue" name="CityAttributeIDVAlue"><option value="">Please Select a City</option>
<option value="277">Aosta</option>
<option value="4156">Meda</option>
<option value="4175">Melegnano</option>
<option value="4310">Milano</option>
<option selected="selected" value="4750">Monza</option> <!--Selected is PRESENT-->
</select></td>

这种方法使用@Html.DropDownListFor

如您所见,默认选择的 ddl 项目未选择,而是选择了可选的(“请选择城市”)。这不是我的意图,加载 ddl 时应该选择“Monza”,并且 HTML 选项标签中应该存在 selected 属性。

我所做的唯一更改是在视图中,这是代码,并使用了 DropDownListFor 帮助器:

查看

    <td>@Html.LabelFor(m => m.addressUser.City.Name)</td>
    <td>@Html.DropDownListFor(m => m.cities, (IEnumerable<SelectListItem>)Model.cities, "Please Select a City")</td>

HTML 生成

<td><select id="cities" name="cities"><option value="">Please Select a City</option>
<option value="277">Aosta</option>
<option value="4156">Meda</option>
<option value="4175">Melegnano</option>
<option value="4310">Milano</option>
<option value="4750">Monza</option>  <!--No selected attribute present-->
</select></td>

问题: 有没有办法使用 DropDownListFor Html 助手在 html ddl 中生成一个 slected 标记,或者唯一的方法是 DropDownList?

【问题讨论】:

    标签: model-view-controller html.dropdownlistfor selected strongly-typed-dataset selectlist


    【解决方案1】:

    我不确定 Select 助手有何不同。我将此列表与视图模型一起传递。

    List<SelectListItem> citySelectList = new List<SelectListItem>();
    cityList = cities.GetAll().OrderBy(x => x.DESCRIPTION).ToList();
    
    foreach (var city in cityList)
    {
        citySelectList .Add(new SelectListItem() 
        {
            Value = city.ID, 
            Text = city.Desc, 
            Selected = true 
        });
    }
    

    然后在视图上:

    @Html.DropDownListFor(x => x.City, Model.CitySelectList, "", new { id = "citySelectID" })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多