的SelectedItem多个DropDownListFor没有被选中 [英] SelectedItem for multiple DropDownListFor not being selected

查看:116
本文介绍了的SelectedItem多个DropDownListFor没有被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个DropDownListFor的和所选择的项目没有显示出来。
存储所选项目的变量无误。

I have multiple DropDownListFor's and the selected item is not showing up. The variable storing the selected item is populated correctly.

继承人的code

模式

查看型号

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

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型号

PulldownItem Model

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

任何想法,为什么下拉没有选择从评估值?
用户需要能够动态地添加评估

Any ideas why the pulldowns do not select the values from Assessments? The user needs to be able to dynamically add assessments.

谢谢!

推荐答案

首先,你不必定义自己的PulldownItem类。您可以使用SelectListItem。所以,你的视图模型将是这样的:

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

顺便说一句,你甚至不通过你的评估必须循环。只要创建一个评价的局部视图,将其命名为Assessment.cshtml,并将其放置在查看/共享/ EditorTemplates,然后在主视图,你可以有:

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)

这篇关于的SelectedItem多个DropDownListFor没有被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆