I am trying to learn mvc, and after much struggle I finally got a DropDownListFor
to work as intented. Here is the working Dropdownlistfor
.
Working 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
is the type of preK program the school is enrolled in. The values are stored 1-10 in the database so the DropDownListFor
will need all values and text. Here is the View.
Working View
@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>
I don't know if there is an easier way, but this is the way I finally got it to work. So when I do this exact same process for this next ViewModel it didn't work! Edit The administrator dropdown for title is not selecting any value, or setting any text. I will also list everything I tried. I am 100 percent sure that the data returned is correct I have verified in the watch that the models property title
evaluates to true when I compare it to SelectListItems value.
Non-Working 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; }
}
Non-Working View
@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>
I have even tried
@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))
and every other combination I can think of with the lambda (m=> m, ...
Edit I have been asked for the action as well. This action returns the Index.cshtml. And both the working view and non-working view are EditorTemplates on that page. I am not going to condense the Action or the View.
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);
}
Containing View / Parent View? Whatever the term is.
@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>
}
EDIT 2
Proof of the data matching the value. I had changed the variable name to listItems2
in the non-working view in case there was some issue there.
Edit 3
This seems to be a Visual studio / .Net issue. I can't step into the accessing of the title property. Intellisense doesn't pick up the title property of the model as well.
When I type @Html.DropDownListFor(m => m.
I get no intellisense for title
. The working DropDownListFor
will step through for the format
property, but it also doesn't have intellisense in the lambda.
When I was originally stepping through, I was looking at this.Model.title
which had the proper value. But the lambda expression does not step into accessing the property!