2013-08-21 24 views
0

我有一个添加/编辑视图与几个dropdownlistfor,所有从同一个选择列表填充。当我尝试为editinng填充视图时,所有dropdownlist都与第一个相同。有没有解决的办法?我不想使用多个选择列表,因为它们都具有相同的内容。多个Html.dropdownlistfor与同一selectlist,设置选定的值

大多数下拉列表是从列表中创建的,每个项目都有一个下拉列表。这是视图的第一部分,它不是从项目列表创建的。

 <div class="control-group"> 
      @Html.LabelFor(model => model.FirstRound, new { @class = "control-label" }) 
      <div class="controls"> 
       @Html.DropDownListFor(model => Model.FirstRound.Id, Model.Difficulties, new { @class = "span3 combobox"}) 
       @Html.TextBoxFor(model => Model.FirstRound.Value, new { @class = "input-xlarge span1 sec-val", @readonly = "readonly"}) 
       @Html.ValidationMessageFor(model => model.FirstRound.Value, null, new { @class = "help-inline" }) 
      </div> 
     </div> 

这是我循环项目列表以创建下拉列表的部分。

@{int counter = 0;} 
     @foreach (var item in Model.SecondRound) 
     { 

      <div class="control-group"> 
       @Html.LabelFor(model => model.SecondRound, new { @class = "control-label" }) 
       <div class="controls second-round"> 
        @Html.DropDownListFor(model => model.SecondRound[counter].Id, Model.Difficulties, new { @class = "span3 combobox", @id = "SecondRound[" + counter + "].Id" }) 
        @Html.TextBoxFor(model => Model.SecondRound[counter].Value, new { @class = "input-xlarge span1 sec-val", @readonly = "readonly"}) 
        @Html.ValidationMessageFor(model => Model.SecondRound[counter].Value, null, new { @class = "help-inline" }) 
       </div> 
      </div> 
      <hr /> 
      counter = counter + 1; 
     } 

这是我的ViewModel

public class AddTariffTrampetVM 
{ 
    public string Team { get; set; } 
    [HiddenInput(DisplayValue=false)] 
    public int NumberOfGymnasts { get; set; } 

    [Display(Name="Difficulty")] 
    public RoundVM FirstRound { get; set; } 
    [Range(0.001d, 100.0d, ErrorMessage="You need to pick a difficulty for the first round!")] 
    public decimal FirstTotalValue { get; set; } 

    [Display(Name = "Difficulty")] 
    public IList<RoundVM> SecondRound { get; set; } 
    [Range(0.001d, 100.0d, ErrorMessage = "You need to pick difficulties for the second round!")] 
    public decimal SecondTotalValue { get; set; } 

    [Display(Name = "Difficulty")] 
    public IList<RoundVM> ThirdRound { get; set; } 
    [Range(0.001d, 100.0d, ErrorMessage = "You need to pick difficulties for the third round!")] 
    public decimal ThirdTotalValue { get; set; } 

    public List<SelectListItem> Difficulties { get; set; } 

    public AddTariffTrampetVM() : this(6) 
    { 
    } 

    public AddTariffTrampetVM(int numOfGymnast) 
    { 
     NumberOfGymnasts = numOfGymnast; 

     FirstRound = new RoundVM { Id = 0, Value = 0M }; 
     SecondRound = new List<RoundVM>(); 
     ThirdRound = new List<RoundVM>(); 
     for (int i = 0; i < NumberOfGymnasts; i++) 
     { 
      SecondRound.Add(new RoundVM()); 
      ThirdRound.Add(new RoundVM()); 
     } 
    } 
} 

public class RoundVM { 
    public int Id { get; set; } 
    [Range(0.001d, 100.0d, ErrorMessage="Your need to pick a difficulty!")] 
    public decimal Value { get; set; } 


    public RoundVM() 
    { 
     Value = 0M; 
    } 
} 

这是我的编辑操作,以填写表格

public ActionResult Edit(int id) 
    { 
     var item = ServiceTariff.GetTariffTrampet(id); 
     var model = Mapper.Map<TariffTrampet, AddTariffTrampetVM>(item); 
     model.Difficulties = ServiceDifficulty.GetSelectListElementTrampet().ToList(); 

     return View("Add", model); 
    } 

希望有人能帮助我与此有关。如果你需要更多的代码只是评论。

回答

3

而不是仅仅提供Model.DifficultiesDropDownListFor助手尝试传递:

new SelectList(Model.Difficulties, object selectedValue) 

或其他重载允许您指定的值和文本字段:

new SelectList(Model.Difficulties, string dataValueField, string dataTextField, object selectedValue) 

使用这些作为一个第二个参数为DropDownListFor帮手。通过从List<SelectListItem>创建SelectList,您可以设置最初选择的值。

+0

谢谢你,添加此行解决了我的问题:新的SelectList(Model.Difficulties,“Value”,“Text”,Model.ThirdRound [counter] .Id) – TobiasW

+0

非常好!很高兴做了这个把戏:) – asymptoticFault

+0

4年后,仍然是一个很大的帮助谢谢! –