1
votes

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.

enter image description here

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.

2
How can you use same select list when you are going to have different options altogether in dropdowns? You need to have different selectlist in model or you can send in ViewBag.ListName.....Nitin Varpe
@Nitin Varpe - I'm not having different options in the dropdowns - I just want the default selected value to be different when rendered in the view. The dropdowns themselves will have the same elements for each categoryJonnus
I'm voting to close this question as off-topic because this was due to a typo, and therefore not likely to be helpful to people in the future.Jonnus

2 Answers

0
votes

Simply, Create a selectlist in partial view and reuse it. for setting default value, pass the default value to partial view as parameter

forexample:

@Html.Partial("YourPartialView", new ViewDataDictionary({ defaultValue : yourvalue }))
0
votes

You can reuse ScoreOptionsSelectList to populate multiple dropdownlists like this:

Populate the category defaults when you create the model. As this is the Edit view, you would populate these from the database after retrieving the record you want to edit. I've used a simple example for demonstration purposes:

model.category1 = 1;
model.category2 = 2;
model.category3 = 3;
model.category4 = 4;

In the view, you use create the controls, using the same object to populate the dropdownlists:

@Html.DropDownListFor(model => model.category1,  Model.ScoreOptionsSelectList) 
@Html.DropDownListFor(model => model.category2,  Model.ScoreOptionsSelectList)
@Html.DropDownListFor(model => model.category3,  Model.ScoreOptionsSelectList)
@Html.DropDownListFor(model => model.category4,  Model.ScoreOptionsSelectList)

The model property dictates the name of the rendered control and therefore the field that will be bound when POSTed. Model.ScoreOptionsSelectList is simply the raw data from which the dropdownlists are created, so there's no need to make multiple database calls to populate them as they're identical.