Reputation: 2505
I am trying to display in a dropdown list box a list of 'scheduled visits' based on previous entries. I am able to gather the information but when i display it the dropdown list box turns into an input box and non of my information that i want are being displayed. The format in which i would like my items to be displayed is by : 10/2/2012 - At School.
Here is what i have for my code:
This is where i gather the info of the visit - date and place
public JsonResult GetVisitDetails(Guid visitEntryId)
{
var model = new VisitDetailModel();
VisitEntry visitEntry = _visitEntryService.Get(visitEntryId);
if(visitEntry == null)
{
model.Message = string.Format(Message.NotFoundMessage, Resources.Entities.Visit.EntityName);
return Json(model);
}
model.VisitEntryId = visitEntryId;
model.VisitTypeId = visitEntry.VisitTypeId;
if (visitEntry.VisitType != null)
model.VisitType = visitEntry.VisitDate.ToShortDateString();
return Json(model);
}
#region Nested Type:VisitDetailModel
public class VisitDetailModel
{
public Guid VisitEntryId { get; set; }
public short VisitTypeId { get; set; }
public string VisitType { get; set; }
public string VisitDate { get; set; }
public string Message { get; set; }
}
#endregion
This is what i have in my AddToViewModel:
var visits = _visitEntryService.FindAllForCase(viewModel.CaseId, _currentUser.OfficeId).Where(v=>v.VisitDate <= DateTime.Today.SetToMaximumTime()).OrderByDescending(v=>v.VisitDate).ToList();
visits.Insert(0, new VisitEntry());
viewModel.Visits = visits.ToSelectList("VisitEntryId", "Display", viewModel.VisitEntryId.ToString());
if(viewModel.VisitTypes.Count() == 0)
ModelState.AddModelError("",string.Format("No active {0} entered.", Kids.Resources.Entities.VisitType.EntityNamePlural));
My .ascx page to show dropdown:
<div class="row">
<%:Html.EditorFor(m=>m.VisitEntryId) %>
<%:Html.LabelFor(m=>m.Visits) %>
<%:Html.ValidationMessageFor(m=>m.VisitEntryId) %>
My viewModel:
[LocalizedDisplayName("VisitEntry", NameResourceType = typeof(VisitActivity))]
public short? VisitEntryId { get; set; }
[UIHint("DropDownList")]
[DropDownList(DropDownListTargetProperty = "VisitEntryId")]
public IEnumerable<SelectListItem> Visits { get; set; }
Any information of why i am not able to display a dropdownlist would be helpful.
Upvotes: 0
Views: 568
Reputation: 19465
In your ascx you're doing EditorFor on model.VisitEntryId
which is an int
You need a Html.DropDownFor
for a drop down list.
Your model.Visits
is a SelectList and I'm guessing you should do:
<%:Html.LabelFor(m=>m.VisitEntryId) %>
<%:Html.EditorFor(m=>m.Visits) %>
<%:Html.ValidationMessageFor(m=>m.VisitEntryId) %>
Upvotes: 1