0

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

StepFour模型

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

任何想法为什么下拉不从评估中选择值?用户需要能够动态添加评估。

谢谢!

4

1 回答 1

2

First of all, you don't have to define your own PulldownItem class. You can use SelectListItem. So, your ViewModel will be like this:

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
}

Then, in your View, your drop down lists will be like this:

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

By the way, you don't even have to loop through your Assessments. Just create a partial view for Assessment, name it Assessment.cshtml, and place it under Views/Shared/EditorTemplates, then in your main View you can have:

@Html.EditorFor(model => model.Assessments)
于 2013-07-24T00:45:00.140 回答