SelectedItem用于多个DropDownListFor未被选中

时间:2013-07-23 22:22:27

标签: c# asp.net-mvc asp.net-mvc-3 dropdownlistfor

我有多个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
}

PulldownItem模型

 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>
                           }

为什么下拉菜单没有从评估中选择值? 用户需要能够动态添加评估。

谢谢!

1 个答案:

答案 0 :(得分:2)

首先,您不必定义自己的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)