jeudi 20 avril 2017

Why does this one DropDownListFor work, and this other one doesnt?

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, ...

Aucun commentaire:

Enregistrer un commentaire