编辑模型时,下拉列表中存在实体“ProgramType”。如果用户在现有集合中没有看到他们需要的内容,他们需要能够在同一编辑屏幕上向该集合添加新条目。
从本质上讲,我似乎需要同一个实体在同一个编辑视图中存在两次。如果为下拉菜单选择了特定值,则文本字段会执行 jQuery“显示”以允许他们添加新数据。
<div class="editor-label">
@Html.LabelFor(model => model.ProgramTypeId, "Program Type")
</div>
<div class="editor-field">
@Html.DropDownList("ProgramTypeId", "- Select a Program Type - ")
@Html.ValidationMessageFor(model => model.ProgramTypeId)
</div>
//this div is hidden via jQuery unless needed
<div class="editor-label hiddenDiv">
@Html.Label("New Program Type Title:")
</div>
<div class="editor-field hiddenDiv">
@Html.EditorFor(model => model.ProgramType.ProgramType)
@Html.ValidationMessageFor(model => model.ProgramType.ProgramType)
</div>
//
<div class="editor-label">
@Html.LabelFor(model => model.ProgramYear)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProgramYear)
@Html.ValidationMessageFor(model => model.ProgramYear)
</div>
实现这一目标的最佳方法是什么?我真的很想只使用表单集合,但这似乎是“糟糕的做法”。
public class SurveyProgramModel
{
[Key]
public int ProgramId { get; set; }
[DisplayName("Year")]
public int ProgramYear { get; set; }
[DisplayName("Status")]
public int ProgramStatusId { get; set; }
[DisplayName("Program Title")]
public string ProgramTitle { get; set; }
public int ProgramTypeId { get; set; }
[DisplayName("Program Type")]
public virtual SurveyProgramTypeModel ProgramType { get; set; }
[DisplayName("Status")]
public virtual ProgramStatusModel ProgramStatusModel { get; set; }
public virtual ICollection<SurveyResponseModel> SurveyResponseModels { get; set; }
}
public class SurveyProgramTypeModel
{
[Key]
public int ProgramTypeId { get; set; }
[DisplayName("Program Type")]
public string ProgramType { get; set; }
}