How to use a drop down list from model in WebGrid MVC C#

70 Views Asked by At

What is the best or easiest way to implement a drop down list using webgrid?

@{
    WebGrid gridBenefits = new WebGrid(Model.Benefits, rowsPerPage: 4);
}

        <div class="row">
            @gridBenefits.GetHtml(
            tableStyle: "table table-responsive table-striped table-bordered",
            columns: gridBenefits.Columns(
                    gridBenefits.Column(header: "Description", format: @<text><div class="edit" data-id="@item.BenefitID" data-identity="Benefits" data-propertyname="BenefitDescription"> @item.BenefitDescription</div></text>),
                    gridBenefits.Column(header: "Progress", format:@<text><div class="edit" data-id="@item.BenefitID" data-identity="Benefits" data-propertyname="Progress"> @item.ProgressID</div></text>)

                    )
                    )
        </div>

My model is below for the progress drop down column:

public class Progress
{
    public int ProgressID { get; set; }
    public string Status { get; set; }
}

I would like to display 'Status' for each ProgressID in the drop down column. For the moment it displays the ProgressID currently, how can I adapt the code to have a drop down list instead?

1

There are 1 best solutions below

0
vika4321 On

I have not tested this, but my thoughts are something like this:

@{
    WebGrid gridBenefits = new WebGrid(Model.Benefits, rowsPerPage: 4);
}

        <div class="row">
            @gridBenefits.GetHtml(
            tableStyle: "table table-responsive table-striped table-bordered",
            columns: gridBenefits.Columns(
                    gridBenefits.Column(header: "Description", format: @<text><div class="edit" data-id="@item.BenefitID" data-identity="Benefits" data-propertyname="BenefitDescription"> @item.BenefitDescription</div></text>),
                    gridBenefits.Column(header: "Progress",  format: (item) => @Html.DropDownList("ProgressId", Model.First().Status.Select(l => new SelectListItem
                          {
                            Text = l.Text,
                            Value = l.Value,
                           Selected = ((WebGridRow)item)["ProgressId"].ToString() == 
                            l.Value
})))
                    )
                    )
        </div>