I have this controller
public ActionResult Create()
{
var db = new MyDbContext();
IEnumerable<SelectListItem> items = db.FileUploadCategories
.Select(c => new SelectListItem {
Value = c.ParentCategoryID.ToString(),
Text = "--" + c.CategoryName
});
ViewBag.Categories = items;
return View();
}
And a view like this
@Html.DropDownList("ParentCategoryID", (IEnumerable<SelectListItem>)ViewBag.Categories, "-- Select --", new { @class = "form-control" })
This gives me the this result on the website
<select>
<option value="0">Test</option>
<option value="1">Test</option>
<option value="1">Test</option>
<option value="2">Test</option>
<option value="2">Test</option>
</select>
As you can see, some value are the same, which is because some ParentCategoryID from the controller is the same which is correct. What I would like to do is add a "-" in the text for each text in the options which will be determined by how large the value is. I want to add 3 ' - ' in text if the value is 3 and if the value is five, I want add five '-----' before the test in text. I want a result similar to this.
<select>
<option value="0">Test</option>
<option value="1">-Test</option>
<option value="1">-Test</option>
<option value="2">--Test</option>
<option value="2">--Test</option>
</select>
Notice the lines before the test.
I have tried to update my controller to
Text = new String('-',c.ParentCategoryID) + c.CategoryName
Which gives me the error message
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: Only parameterless constructors and initializers are supported in LINQ to Entities.