.net - Only parameterless constructors and initializers are supported in LINQ to Entitier in dropdown -
i have 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 view this
@html.dropdownlist("parentcategoryid", (ienumerable<selectlistitem>)viewbag.categories, "-- select --", new { @class = "form-control" })
this gives me result on 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 can see, value same, because parentcategoryid controller same correct. add "-" in text each text in options determined how large value is. want add 3 ' - ' in text if value 3 , if value five, want add 5 '-----' before test in text. want result similar 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 lines before test.
i have tried update controller to
text = new string('-',c.parentcategoryid) + c.categoryname
which gives me error message
an exception of type 'system.notsupportedexception' occurred in entityframework.sqlserver.dll not handled in user code
additional information: parameterless constructors , initializers supported in linq entities.
you query needs able represented sql statement, new string('-',c.parentcategoryid)
can't be. materialize query list<t>
first
var items = db.fileuploadcategories.tolist().select(c => new selectlistitem() { value = c.parentcategoryid.tostring(), text= string.format("{0}{1}", new string('-',c.parentcategoryid), c.categoryname) }).tolist();
Comments
Post a Comment