0
votes

I have a collection that I populate in the controller MenuitemDetails and this populates a property on the view model.

The collection has 3 properties and multiple records Id, Title and State

State is an integer value which relates to a dropdown list selected value.

The viewmodel also contains a selectList property (stateList) which is used to populate the dropdownlist items on the view.

I'm trying to repopulate the form for an edit action - with the same dropdown item selections

 <table>
        @for (int counter = 0; counter < Model.MenuitemDetails.Count; counter++)
        {                
            <tr>
                <td>
                    @Model.MenuitemDetails[counter].Title
                    @Html.DropDownListFor(i => Model.MenuitemDetails[counter].state, Model.stateList)                        
                </td>
                <hr />
            </tr>
        }                
    </table>

I can't seem to get the values of the dropdown to display the appropriate values selected.

2

2 Answers

0
votes

Rather than pass in a select list in the viewmodel it can be created in the razor view and in this way you can use the overload for creating the select list to determine the value to bind.

In the example I am using telerik mvc dropdown list but the same applies in terms of creating the selectList in the razor and with a property to bind to which will determine its state.

           @(Html.Kendo().DropDownListFor(m => m.Entity.ProductId)
                                      .DataTextField("Text")
                                      .DataValueField("Value")
                                      .Filter("contains")
                                      .HtmlAttributes(new { @class = "form-control" })
                                      .BindTo(new SelectList(Model.Products, "Id", "FullName", Model.Entity.ProductId)))
0
votes

You can also try to use an editortemplate for this sort of things like so:

@(Html.Kendo().DropDownListFor(m => m)
                       .OptionLabel("--Select Value--")
                       .DataSource(source =>
                       {
                           source.Read(read =>
                           {
                               read.Action("MethodName", "ControllerName").Data("dataforMethod");
                           });

                       })
                       .Enable(true)
                       .AutoBind(true)
  )

In the .Data() part on the read you dont need to use it only if you have some parameters you need int the funtion to get the dataset