未选择多个DropDownListFor的SelectedItem

本文关键字:SelectedItem DropDownListFor 选择 | 更新日期: 2023-09-27 18:24:28

我有多个DropDownListFor,所选项目没有显示。存储所选项目的变量已正确填充。

这是代码

型号

查看模型

public class StepFourView
{
    #region Public Properties
    public IEnumerable<PulldownItem> Levels { get; set; }
    public IEnumerable<PulldownItem> Approaches { get; set; }
    public IEnumerable<PulldownItem> Types { get; set; }
    public StepFour StepFour{ get; set; }
    #endregion
}

StepFourModel

[Table(Name = "StepFours")]
public class StepFour : ICarStep
{
    #region Fields
    /// <summary>
    ///   The attachments
    /// </summary>
    private readonly EntitySet<Assessment> assessments = new EntitySet<Assessment>();
    /// <summary>
    ///   The update check.
    /// </summary>
    private string updateCheck;
    #endregion
    #region Public Properties
    /// <summary>
    ///   Gets the attachments.
    /// </summary>
    [Association(ThisKey = "ReportId", Storage = "assessments", OtherKey = "ReportId")]
    public EntitySet<Assessment> Assessments
    {
        get
        {
            return this.assessments;
        }
    }

评估模型

[Table(Name = "Assessments")]
public class Assessment
{
    #region Public Properties

    /// <summary>
    ///   Gets or sets the id.
    /// </summary>
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int Id { get; set; }
    /// <summary>
    /// Gets or sets the report id.
    /// </summary>
    [Column(UpdateCheck = UpdateCheck.Never)]
    public int ReportId { get; set; }
    /// <summary>
    ///   Gets or sets the type id.
    /// </summary>
    [Column(UpdateCheck = UpdateCheck.Never)]
    public int TypeId { get; set; }
    /// <summary>
    ///   Gets or sets the level id.
    /// </summary>
    [Column(UpdateCheck = UpdateCheck.Never)]
    public int LevelId { get; set; }
    /// <summary>
    ///   Gets or sets the approach id.
    /// </summary>
    [Column(UpdateCheck = UpdateCheck.Never)]
    public int ApproachId { get; set; }
    /// <summary>
    ///   Gets or sets the program area.
    /// </summary>
    [Column(UpdateCheck = UpdateCheck.Never)]
    public string ProgramArea { get; set; }
    #endregion
}

下拉项型号

 public class PulldownItem 
{
    public int Value { get; set; }
    public string Text { get; set; }
}

查看

 @for (int i = 0 ; i < this.Model.StepFour.Assessments.Count ;  i++ )
                           {
                               @Html.HiddenFor((x) => x.StepFour.Assessments[i].Id)
                               @Html.HiddenFor((x) =>             
                                               x.StepFour.Assessments[i].ReportId)
                               <tr id="program_area1">
                                   <td>
                                       @Html.TextBoxFor(x => 
                 x.StepFour.Assessments[i].ProgramArea, new { style = "width: 190px;" })
                                   </td>
                                   <td>
                                       @Html.DropDownListFor(x => 
                                         x.StepFour.Assessments[i].LevelId, new 
                  SelectList(this.Model.Levels, "Value", "Text"), new { })
                                       @Html.HiddenFor(x => 
                                    x.StepFour.Assessments[i].LevelId)
                                   </td>
                                   <td>
                                       @Html.DropDownListFor(x => 
 x.StepFour.Assessments[i].TypeId, new SelectList(this.Model.Types, "Value", "Text"),   
 new { })
                                    @Html.HiddenFor(x => 
 x.StepFour.Assessments[i].TypeId)
                                   </td>
                                   <td>
                                       @Html.DropDownListFor(x => 
  x.StepFour.Assessments[i].ApproachId, new SelectList(this.Model.Approaches, "Value", 
  "Text"), new {})
                                   @Html.HiddenFor(x => 
   x.StepFour.Assessments[i].ApproachId)
                                   </td>
                               </tr>
                           }

为什么下拉列表没有从评估中选择值,有什么想法吗?用户需要能够动态添加评估。

谢谢!

未选择多个DropDownListFor的SelectedItem

首先,您不必定义自己的PulldownItem类。您可以使用SelectListItem。因此,您的ViewModel将是这样的:

public class StepFourView
{
    #region Public Properties
    public IEnumerable<SelectListItem> Levels { get; set; }
    public IEnumerable<SelectListItem> Approaches { get; set; }
    public IEnumerable<SelectListItem> Types { get; set; }
    public StepFour StepFour{ get; set; }
    #endregion
}

然后,在您的视图中,您的下拉列表将如下所示:

@Html.DropDownListFor(x => 
  x.StepFour.Assessments[i].ApproachId, new SelectList(this.Model.Approaches, "Value", 
  "Text", Model.StepFour.Assessments[i].AppreachId))

顺便说一句,您甚至不必循环完成评估。只需为Assessment创建一个局部视图,将其命名为Assessment.cshtml,并将其放置在Views/Shared/EditorTemplates下,然后在主视图中可以有:

@Html.EditorFor(model => model.Assessments)