I have a SelectList that I use to populate the dropdowns in my View, to give a category a score from 1 to 5. There are 7 categories, that can all have a separate score.
I’ve successfully created a view that will display the SelectList as a dropdown for each item, and assign the selected value for the category when I create a new object – but I’m struggling with the corresponding edit view.
I assumed it would be best to reuse the dropdown again, and set the default value to be the value from the database for the applicable category. It doesn’t seem to be possible to set the default value in the view however – the default can only be set when first creating the SelectList.
I created the SelectList as follows
model.ScoreOptionsSelectList = _tl_RangeService.List()
.Select(s => new SelectListItem
{
Text = s.CustSatRange,
Value = s.CustSatRange
})
.ToList();
Where _tl_RangeService.List()
is returning a list of items with unique values defining the CustSatRange
(for the purposes of this example these numbers fall in the range 1 to 5).
And then I use it in the view as
<td class="@item.LatestScoreStyle">
@Html.DropDownListFor(
model => model.Manage_Rating,
Model.ScoreOptionsSelectList,
null,
new { @id = "ddlScores" + @rowIndex, @class = "form-control" })
</td>
Where I am trying to set the default value to be the current Manage_Rating
, and so it is Manage_Rating
that will be updated when the form is submitted.
This currently renders as below. There will be a number of other categories other than Management that will all use the same SelectList to populate the dropdown. Note the Survey Type is working successfully as it is a SelectList that only applies to a single category.
Is there a way to create a single SelectList to reuse to populate dropdowns, yet set the default value differently for each instance when it is created in the view? Or am I forced to create a new SelectList for each category?
Any suggestions you could provide are appreciated
Edit
So after a full day of head-scratching (hence the question on here), when I was trying to provide further clarification I discovered I'd made a rookie mistake, and not set the Managment_Rating
in the first place. D'oh!
Therefore as this was a typo, I'm voting to close this question.
Thanks to those who attempted to assist me with this. Sorry for wasting your time.