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
}

ステップフォーモデル

[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

まず、独自の 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)
于 2013-07-24T00:45:00.140 に答える