adelb
adelb

Reputation: 821

Set dropdownlist selected value in a table based on another cell (MVC)

I want to create an 'editable' table in MVC, where each row will have its own dropdownlist to update the row value. How do I set the dropdownlist selected value on page load for rows that were already updated before.

My current Razor code is like this:

<tbody>
   @foreach (var conversion in Model.PaginatedConversions)
   {
        <tr>
             <td>@conversion.tSystem.systemName</td>
             <td>@conversion.conversionStandardValue</td>
             <td>@Html.DropDownList("ConversionStandardIdFor" + conversion.conversionID.ToString(), Model.AvailableValuesForConversion, "")</td>
             <td>@conversion.conversionDescription</td>
             <td>@conversion.conversionDateInput</td>
             <td>@conversion.ConversionDateConverted</td>
             <td>@conversion.ConversionUser</td>
        </tr>
   }

But I cannot set the selected value of each dropdown. How can I do this? Thanks for the help!

Edit1 : This code loops a model property (Model.PaginatedConversions), which is of type List<Conversion>. The Conversion class is as followed:

public class Conversion {
    int ConversionId { get;set; }
    string ConversionValue { get; set; }
    int ConversionConvertedValue { get; set; }
}

The goal of the table is to create editable rows for user to map the ConversionValue to another value that can be selected in Model.AvailableConversionValues. This converted value will be stored in ConversionConvertedValue.

The property Model.AvailableConversionValues are of type List<SelectListItem> that contains the available values user can choose from to map the ConversionValue.

Upvotes: 1

Views: 6806

Answers (2)

adelb
adelb

Reputation: 821

Passing the ConversionValue as a name didn't work. I created another method in the ViewModel, and use that instead for getting the SelectListItem, passing the selected value to be selected in the ListItem.

<td>@Html.DropDownList("ConversionFor" + conversion.conversionID.ToString(), Model.GetAvailableConversionValues(conversion.conversionStandardID))</td>

And in the ViewModel I have this method:

public List<SelectListItem> GetAvailableConversionValues(int? selectedValue)
    {
        if (selectedValue != null)
        {
            SelectListItem selectItem = AvailableValuesForConversion.Where(t => int.Parse(t.Value) == selectedValue).SingleOrDefault();

            if (selectItem != null)
            {
                selectItem.Selected = true;
            }
        }
        return AvailableValuesForConversion;
    }

Thanks for the help!

Upvotes: 2

VJAI
VJAI

Reputation: 32768

You can use the DropDownListFor html helper by passing the option's value that has to be selected as the first parameter and the collection as the second.

@Html.DropDownListFor(m => m.LastSelected, Model.AvailableValuesForConversion, "")

DropDownList setting selected item in asp.net MVC

razor Html.DropdownList provide "Please select" as optionLabel AND "US" as selectedValue

UPDATE:

There may be a good solution but this is what I come up.

The thing is the DropDownList method searches the selected value based on the passed name in the Model, ViewData etc. So the idea is add a property in the ViewData with the dropdown's name and set the value to the one that you need to set in the dropdown and you are done!

I thing you have to set the ConversionValue to the dropdown else update the code accordingly.

   @foreach (var conversion in Model.PaginatedConversions)
   {
        var ddId = "ConversionStandardIdFor" + conversion.conversionID.ToString();

        ViewData[ddId] = conversion.ConversionValue;

        <tr>
             <td>@conversion.tSystem.systemName</td>
             <td>@conversion.conversionStandardValue</td>
             <td>@Html.DropDownList(ddId, Model.AvailableValuesForConversion, "")</td>
             <td>@conversion.conversionDescription</td>
             <td>@conversion.conversionDateInput</td>
             <td>@conversion.ConversionDateConverted</td>
             <td>@conversion.ConversionUser</td>
        </tr>
   }

Upvotes: 0

Related Questions