Search code examples
asp.net-mvcwebgrid

Dropdownlist in WebGrid - MVC4


Can someone tell me what I am doing wrong in the View for Dropdownlist? I think it is expecting SelectList for dropdownlist? Thank you in advance.

My Model:

public class AppointmentModel
    {
        public int CustomerID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }        
        public List<DateTime> ApptDates { get; set; }
    }

View:

@{
    var grid = new WebGrid(Model, rowsPerPage: ViewBag.RowsPerPage);    
    @grid.GetHtml(columns: grid.Columns(
        grid.Column(columnName: "LastName", header: "Last Name", canSort: true, format: @<text>@item.LastName</text>),
        grid.Column(columnName: "LastName", header: "First Name", canSort: true, format: @<text>@item.Firstname</text>),
        grid.Column(columnName: "ApptDates", header: "Appointment Dates", format: @Html.DropDownList(@item.ApptDates))
        ));                                             
}

The following changes fixed the issue:

public class AppointmentModel
    {
        public int CustomerID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }        
        public DateTime ApptDt { get; set; }
        public IEnumerable<SelectListItem> ApptDatesSelectList { get; set; }
    }

View

@{
    var grid = new WebGrid(Model, rowsPerPage: ViewBag.RowsPerPage);    
    @grid.GetHtml(columns: grid.Columns(
        grid.Column(columnName: "LastName", header: "Last Name", canSort: true, format: @<text>@item.LastName</text>),
        grid.Column(columnName: "LastName", header: "First Name", canSort: true, format: @<text>@item.Firstname</text>),
        grid.Column(columnName: "ApptDates", header: "Appointment Dates", format: @<text>@Html.DropDownList("ApptDt", (IEnumerable<SelectListItem>)@item.ApptDatesSelectList)</text>)
        ));                                             
}

Solution

  • Assuming that the answer to my comment was that each appointment has one date, you first need to add a property to your model for the specific date:

    public class AppointmentModel
        {
            public int CustomerID { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public DateTime AppointmentDate {get; set;}        
            public List<DateTime> ApptDates { get; set; }
        }
    

    Then you will need your potential appointments in a SelectList once you have done that, you can implement like so:

    @{
        var grid = new WebGrid(Model, rowsPerPage: ViewBag.RowsPerPage);    
        @grid.GetHtml(columns: grid.Columns(
            grid.Column(columnName: "LastName", header: "Last Name", canSort: true, format: @<text>@item.LastName</text>),
            grid.Column(columnName: "LastName", header: "First Name", canSort: true, format: @<text>@item.Firstname</text>),
            grid.Column(columnName: "ApptDates", header: "Appointment Dates", format: @<text> @Html.DropDownList(@item.AppointmentDate, yourSelectList )</text>)
            ));                                             
    }