我有一个看起来像的对象:
public class Emailing
{
public string EmailName { get; set; }
public List<string> EmailRecipients { get; set; }
}
我希望有一个显示EmailName
列表的下拉列表,并且根据EmailName
,我想显示用户可以编辑的EmailRecipients
个文本框的数量。
显示EmailName列表工作正常。但是,我无法在下拉列表中选择后显示文本框。我猜测我需要在EmailName
下拉列表中进行某种Jquery事件,但我不确定要放在那里。
我目前的观点:
@Html.DropDownListFor(m => m.SelectedEmailName, Model.EmailingJobsSelectList, EmailingManagementViewModel.DefaultEmailNameListMessage, new { style = "width:250px", @class = "form-control" })
@if (!string.IsNullOrEmpty(Model.SelectedEmailName))
{
foreach (var recipent in Model.EmailingJobs.FirstOrDefault(r => r.EmailName == Model.SelectedEmailName).EmailRecipients)
{
@Html.LabelFor(model => recipent, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => recipent)
@Html.ValidationMessageFor(model => recipent)
}
}
Jquery:
<script type="text/javascript">
$(document).ready(function () {
$("#SelectedEmailName").change(function () {
if ($("#SelectedEmailtName").val() !== "" && $("#SelectedEmailName").val() !== null) {
//Something goes here??
} else {
}
return false;
});
});
</script>
呈现的HTML代码:
<select class="form-control" id="SelectedEmailName" name="SelectedEmailtName" style="width:250px"><option value="">Please select an email name</option>
<option value="Email">Email</option>
<option value="Email2">Email2</option>
</select>
我无法获取收件人部分的呈现html,SelectedEmailName
为NULL
,因此首次加载页面时,收件人文本框不会显示任何内容。