3

This is my controller class

public class HomeController : Controller
{
    private rikuEntities rk = new rikuEntities();
    public ActionResult Index()
    {
        var db = new rikuEntities();
        IEnumerable<SelectListItem> items = db.emp.Select(c => new     
               SelectListItem
               {
                 Value = c.Id.ToString(), 
                 Text = c.name
               });
        ViewBag.CategoryID = items;
        return View();
    }
}

this is my view

@using (Html.BeginForm("viewToController", "Home"))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>emp</legend>

        <div class="editor-field">
            @Html.DropDownList("CategoryID", (IEnumerable<SelectListItem>) ViewBag.Categories)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

whenever I run this program I get this error:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression." in the statement @Html.DropDownList("CategoryID", (IEnumerable) ViewBag.Categories). i am using entity framework mechanism for databse connection. please help me to find out the error...

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Pushpendra Kuntal
  • 6,118
  • 20
  • 69
  • 119
  • I am having the same issue, I still don't know how to do the ".ToString()" method inside the expression. It returns the error: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression – Rafael Fernandes Jan 02 '13 at 13:04
  • There is a good answer in the topic bellow: http://stackoverflow.com/questions/10110266/why-linq-to-entities-does-not-recognize-the-method-system-string-tostring – Rafael Fernandes Jan 02 '13 at 13:14
  • possible duplicate of [LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression](http://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method) – Alex Jun 09 '14 at 16:07

2 Answers2

4

I would recommend you to use view models and strongly typed views instead of ViewBag. So start with defining your view model:

public class EmployeeViewModel
{
    public string CategoryId { get; set; }
    public IEnumerable<Employee> Categories { get; set; }
}

then in the controller populate this view model:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var db = new rikuEntities();
        var model = new EmployeeViewModel
        {
            Categories = db.emp.ToArray() // <-- you probably want categories here
        };
        return View(model);
    }
}

and in the view:

@model EmployeeViewModel
@using (Html.BeginForm("viewToController", "Home"))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>emp</legend>

        <div class="editor-field">
            @Html.DropDownListFor(
                x => x.CategoryId,
                new SelectList(Model.Categories, "Id", "name")
            )
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

Sadly EF does not know how to convert ToString() to an SQL statement.

You must, therefore, use the embedded function SqlFunctions.StringConvert.

There is no overload for int so you will need to typecast to double :-(

var items = from v in db.emp  
   select new SelectListItem
   {     
       Text = c.name,        
       Code = SqlFunctions.StringConvert((double)c.Id)
   }; 
kmp
  • 10,535
  • 11
  • 75
  • 125
Muhammad Alaa
  • 608
  • 1
  • 10
  • 15