1

In my project the DropDownListFor(x => x code sits in an EditorTemplate. It is used to populate a table of data one of the fields of the table is the drop down list. Whilst everything is rendering with no issues, the drop down list does not default to the pre-selected item I am settings in the ViewModel. What am I not seeing?

Code as follows:

ViewModel:

public class FooDetailViewModel : ViewModelBase
{
    public List<FooPermissionObject> FooPermissions { get; set; }
}

The Strongly-typed model object:

public class FooPermissionObject
{
    public string Name { get; set; }
    public int Reason { get; set; }
    public IEnumerable<SelectListItem> Reasons { get; set; }
    public bool Selected { get; set; }
}

The controller:

var viewModel = new StockLineManagementDetailViewModel();

using (_model)
{
    foreach (var company in _model.GetAllRecords<Company>())
    {
        var permissionModel = new FooPermissionObject
        {
            Name = company.Name,
            Selected = true,
            Reasons = _model.GetAllRecords<FooPermissionReason>()
                    .ToList()
                    .Select(x => new SelectListItem
                    {
                        Value = x.FooPermissionReasonId.ToString(),
                        Text = x.FooPermissionReasonDesc
                    }),
            Reason = record.FooPermissionReasonId
        };

        viewModel.FooPermissions.Add(permissionModel);
   }
}

The View:

<table id="myTable" class="tablesorter" style="width:98%">
    <thead>
         <tr>
        <th>
            Name
        </th>
        <th>
            Excluded
        </th>
        <th>
            Reason for Exclusion
        </th>
       </tr>
    </thead>
    <tbody>
        @Html.EditorFor(x => x.FooPermissions)
    </tbody>
</table>

The EditorTemplate:

@model FooPermissionObject
<tr>
    <td>
        @Html.DisplayFor(x => x.Name, new { @readonly = "readonly"})
        @Html.HiddenFor(x => x.Name)
    </td>
    <td>
        @Html.CheckBoxFor(x => x.Selected)
    </td>
    <td>
        @Html.DropDownListFor(x => x.Reason, Model.Reasons)
    </td>
</tr>

Anyone got any ideas why this wouldn't populate the DropDownListFor with the Object represented by the Reason value from the Reasons collection?

Charles
  • 50,943
  • 13
  • 104
  • 142
M05Pr1mty
  • 731
  • 9
  • 29

2 Answers2

1

I can't see any code where you are setting selected = true in your select list. You are setting the Selected property of your FooPermissionObject but that is irrelevant your drop down list is bound to the reasons collection. You want something like this:

.Select(x => new SelectListItem
{
    Value = x.FooPermissionReasonId.ToString(),
    Text = x.FooPermissionReasonDesc,
    Selected = (Some codition or other)
})

Replacing some condition or other with whatever your criteria is to say which item should be selected.

EDIT:

A better way might be as below:

Reasons = new SelectList(_model.GetAllRecords<FooPermissionReason>(),
                         "FooPermissionReasonId",
                         "FooPermissionReasonDesc",
                         record.FooPermissionReasonId)

The params to the constructor of SelectList are: Collection to bind, value field, text field, selected value.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
  • As far as i was aware (this may well be wrong) settings the Html helper as Html.DropDownListFor(x => x.Reason, Model.Reasons) auto bound the Selected item as the Reason field? – M05Pr1mty Nov 24 '11 at 13:58
  • Yes the value that the user selects will be the value that gets set in your model for the reason property, but that has nothing to do with pre selecting an item. – Ben Robinson Nov 24 '11 at 14:06
  • Oh, really? So even following the advice of dasheddot would'nt work, as I'm still only setting the field that gets bound when the user selects something? – M05Pr1mty Nov 24 '11 at 14:10
  • His first suggestion would not work, his "An other way" is what i have suggested. There is another way as well i will update. – Ben Robinson Nov 24 '11 at 14:36
  • Yep, like it. That works nicely and is succinct. – M05Pr1mty Nov 24 '11 at 14:51
0

Edited An other way (now the one way) would be to set the Selected property while projecting to the SelectListItem-List if needed.

Nice article about this topic can be found there: http://codeclimber.net.nz/archive/2009/08/10/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx

dasheddot
  • 2,936
  • 4
  • 26
  • 33