I have a code as shown below. I got a runtime exception:
A "System.NotSupportedException" type exception occurred in System.Data.Entity.dll, but was not processed in the user code
Additional information: LINQ to Entities does not recognize the "System.String ToString ()" method, and this method cannot be translated into a storage expression. "
The exception reason for "Value = sup.SupplierID.ToString()" and "Value = cat.CategoryID.ToString()" It seems the framework entity cannot generate an expression tree using ToString() , but for SelectListItem I need to give it a string.
Who can provide a simple solution will be very valuable.
namespace MvcApplication1.Models { public class ProductEditViewModel : Product { public IEnumerable<SelectListItem> SupplierDropDownItems { get; set; } public IEnumerable<SelectListItem> CategorieDropDownitems { get; set; } } } [HttpGet] public ActionResult ProductEdit(Int32 ProductId) { var northwind = new NorthwindEntities(); var q = from p in northwind.Products where p.ProductID == ProductId select new ProductEditViewModel { ProductID = p.ProductID, ProductName = p.ProductName, UnitPrice = p.UnitPrice, SupplierDropDownItems = from sup in northwind.Suppliers select new SelectListItem { Text = sup.CompanyName, Value = sup.SupplierID.ToString(), Selected = p.Supplier.SupplierID == p.SupplierID }, CategorieDropDownitems = from cat in northwind.Categories select new SelectListItem { Text = cat.CategoryName, Value = cat.CategoryID.ToString(), Selected = p.Category.CategoryID == p.CategoryID }, Discontinued = p.Discontinued }; return View(q.SingleOrDefault()); } <div class="form-group"> @Html.LabelFor(model => model.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("SupplierID", Model.SupplierDropDownItems, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.SupplierID, "", new { @class = "text-danger" }) </div> </div>
source share