Controller:
ViewBag.Category = new SelectList(this.service.GetAllCategories(), product.Category);
I do not use Id + Name. GetAllCategories () returns only a few int numbers.
When I use in a view:
@Html.DropDownList("Category", String.Empty)
everything works. Editing is normal, and DropDownList shows the selected value. HTML Result:
<select id="Category" name="Category"> <option value=""></option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option selected="selected">5</option> ... </select>
But I need to use css @class, so I use this code:
@Html.DropDownList("Category", (IEnumerable<SelectListItem>)ViewBag.Category, new { @class = "anything" })
HTML Result:
<select class="anything" id="Category" name="Category"> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> ... </select>
Unfortunately, Edit still works (I can save the value that I select), but DropDownList starts showing the default value, not the value stored in the database .
Do you have any ideas what the problem is?
Update I clarified what to clarify.
The GetAllCategories method is as follows:
public List<int> GetAllCategories() { List<int> categories = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; return categories; }