我正在努力学习mvc,经过多次努力,我终于得到了DropDownListFor
的工作。这是工作Dropdownlistfor
。
使用ViewModel
public partial class get_preKschedule_Result
{
public string start_time { get; set; }
public Nullable<short> lunch_minutes { get; set; }
public string dismiss_time { get; set; }
public Nullable<short> headct { get; set; }
public byte format { get; set; }
public Nullable<byte> days_per_week { get; set; }
public string description { get; set; }
}
format
是学校注册的preK程序的类型。这些值在数据库中存储1-10,因此DropDownListFor
将需要所有值和文本。这是视图。
工作视图
@model DirectoryMVC.Models.get_preKschedule_Result
@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem { Text = "Head Start", Value = "6" });
listItems.Add(new SelectListItem { Text = "Four Year Old At Risk", Value = "7" });
listItems.Add(new SelectListItem { Text = "Integrated Special Ed", Value = "8" });
listItems.Add(new SelectListItem { Text = "Reverse Mainstream Special Ed", Value = "9" });
listItems.Add(new SelectListItem { Text = "All Other District Special Ed", Value = "10" });
}
<div class="col-md-2">
@Html.DropDownListFor(m => m.format, new SelectList(listItems, "Value", "Text", listItems.Where(x => x.Value == Model.format.ToString())))
@*listItems*@
</div>
我不知道是否有更简单的方法,但这是我最终让它发挥作用的方式。因此,当我为下一个ViewModel执行完全相同的过程时,它无法正常工作! 修改标题的管理员下拉列表未选择任何值或设置任何文本。我还会列出我尝试过的所有内容。我百分之百确定返回的数据是否正确我在监视中验证了模型属性title
在将其与SelectListItems值进行比较时的计算结果为真。
非工作ViewModel
public partial class get_administrators_Result
{
public int admin_id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string title { get; set; }
public string email { get; set; }
}
非工作视图
@model DirectoryMVC.Models.get_administrators_Result
@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem { Text = "Mr.", Value = "Mr." });
listItems.Add(new SelectListItem { Text = "Ms.", Value = "Ms." });
listItems.Add(new SelectListItem { Text = "Dr.", Value = "Dr." });
listItems.Add(new SelectListItem { Text = "Miss", Value = "Miss" });
listItems.Add(new SelectListItem { Text = "Mrs.", Value = "Mrs." });
listItems.Add(new SelectListItem { Text = "Rev.", Value = "Rev." });
}
<div class="col-md-1"> @Html.DropDownListFor(m => m.title, new SelectList(listItems, "Value", "Text", listItems.Where(x=>x.Value == Model.title)))
</div>
我甚至尝试过
@Html.DropDownListFor(m => m.title, new SelectList(listItems, "Value", "Text", Model.title))
@Html.DropDownListFor(m => m.title, new SelectList(listItems, "Value", "Text", (object)Model.title))
以及我能用lambda (m=> m, ...
修改 我也被要求采取行动。此操作返回Index.cshtml。工作视图和非工作视图都是该页面上的EditorTemplates。我不会压缩动作或视图。
ActionMethod
public ActionResult Index(string district)
{
getbuilding1_Result bldgResult = de.getbuilding1(district, "1844", null).FirstOrDefault();
List<get_bldg_contact_email_Result> studentExchangeContact = de.get_bldg_contact_email(district, "1844", 1).ToList();
get_grades_offered_Result gradesOffered = de.get_grades_offered(district, "1844").FirstOrDefault();
get_daily_schedule_Result dailyResult = de.get_daily_schedule(district, "1844", 2017).FirstOrDefault();
List<get_administrators_Result> adminResult = de.get_administrators(district, "1844", 0).ToList();
List<getabbrev_Result> abbrResult = de.getabbrev().ToList();
get_total_school_days_Result schoolDays = de.get_total_school_days(district, "1844", 2017).FirstOrDefault();
List<get_preKschedule_Result> kinderPreKResult = de.get_session_schedule(district, "1844", 2017).ToList();
if (studentExchangeContact.Count < 1) { studentExchangeContact.Add(new get_bldg_contact_email_Result()); }
MergeBuilding mergeBuild = new MergeBuilding();
mergeBuild.schoolDays = schoolDays;
mergeBuild.gradesOffered = gradesOffered;
mergeBuild.dailyResult = dailyResult;
mergeBuild.bldgResult = bldgResult;
mergeBuild.studentExchangeContact = studentExchangeContact;
mergeBuild.abbrResult = abbrResult;
mergeBuild.adminResult = adminResult;
mergeBuild.kinderPreKResult = kinderPreKResult;
return View(mergeBuild);
}
包含视图/父视图?无论这个术语是什么。
@using DirectoryMVC.Models
@model MergeBuilding
@{
ViewBag.Title = "Building";
Layout = "~/Views/Shared/_Layout.cshtml";
List<get_preKschedule_Result> prekSession = Model.kinderPreKResult.Where(x => x.format >= 6).ToList();
List<get_preKschedule_Result_Result> kinderSession = Model.kinderPreKResult.Where(x => x.format <= 5).ToList();
List<get_administrators_Result> adminy = Model.adminResult;
}
<h2>Building</h2>
<h4>@Model.bldgResult.bldg_name</h4>
@using (Html.BeginForm("UpdateBuilding", "Building", FormMethod.Post))
{
<div class="row">
<div class="col-md-12">
@Html.EditorFor(m => m.bldgResult, "_FullBuilding")
</div>
</div>
<hr />
<h4>Student Record Exchange Contact</h4>
<div class="row">
<div class="col-md-12">
@for (var i = 0; i < Model.studentExchangeContact.Count; i++)
{
@Html.EditorFor(m => m.studentExchangeContact[i], "_StudentRecordExchange")
}
</div>
</div>
<hr />
<div class="row">
<div class="col-md-12">
<p class="radio-inline"> Which email address do you want printed in the directory?</p>
<label class="radio-inline">@Html.RadioButtonFor(m => m.bldgResult.print_email, "b") Building Email</label>
<label class="radio-inline">@Html.RadioButtonFor(m => m.bldgResult.print_email, "p") Principal/Head Teacher</label>
</div>
</div>
<h4>Administrators</h4>
<div class="row">
<div class="col-md-1">
Title
</div>
<div class="col-md-2">
First name
</div>
<div class="col-md-2">
Last name
</div>
<div class="col-md-3">
Admin code
</div>
<div class="col-md-3">
Email
</div>
</div>
<div class="row">
<div class="col-md-12">
@for (var i = 0; i < adminy.Count; i++)
{
<div class="row">
@Html.EditorFor(m => adminy[i], "_Administrator")
</div>
}
</div>
</div>
<hr />
<div class="row">
<div class="col-md-12">
@Html.EditorFor(m => m.gradesOffered, "_Grades")
</div>
</div>
<div class="row">
<div class="col-md-12">
@Html.EditorFor(m => m.bldgResult, "_BuildingProperties")
</div>
</div>
<hr />
<div class="row">
<div class="col-md-12">
@Html.EditorFor(m => m.dailyResult, "_DailySchedule")
</div>
</div>
<br />
<div class="row">
<div class="col-md-12">
@Html.EditorFor(m => m.schoolDays, "_TotalSchoolDays")
</div>
</div>
<hr />
<h4>Preschool Sessions</h4>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-2">
Start
</div>
<div class="col-md-2">
Lunch Minutes
</div>
<div class="col-md-2">
Dismiss
</div>
<div class="col-md-2">
Days Per Week
</div>
<div class="col-md-3">
PreK Program
</div>
</div>
@for (var i = 0; i < prekSession.Count; i++)
{
<div class="row">
@Html.EditorFor(m => prekSession[i], "_PreKSession")
</div>
}
</div>
</div>
}
编辑2
与数值匹配的数据证明。我在非工作视图中将变量名称更改为listItems2
,以防出现问题。
编辑3
这似乎是Visual studio / .Net问题。我无法进入title属性。 Intellisense也不会获取模型的title属性。
当我输入@Html.DropDownListFor(m => m.
时,我的title
没有智能感知。工作DropDownListFor
将逐步执行format
属性,但它在lambda中也没有intellisense。
当我最初踩过时,我看着this.Model.title
具有适当的价值。但lambda表达式并没有进入访问属性!