开发者

LINQ to Entities error when trying to bind a dropdownlist

开发者 https://www.devze.com 2023-02-09 16:23 出处:网络
I am trying to create a viewmodel a开发者_如何学Gond add a SelectListItem to allow me to bind a dropdown list.

I am trying to create a viewmodel a开发者_如何学Gond add a SelectListItem to allow me to bind a dropdown list. I have a very basic viewmodel that looks like this

  public class CreatePurchaseViewModel
{
    public Product Product { get; set; }
    public IEnumerable<SelectListItem> Products { get; set; }
    public int SelectedProductId { get; set; }
    public DateTime OrderDate { get; set; }
    public bool OrderSent { get; set; }

}

My controller looks like this

 [HttpGet]
    public ActionResult Create()
    {
        var model = new CreatePurchaseViewModel
                        {
                            Products = context.Products.Select(x =>
                                                        new SelectListItem()
                                                            {
                                                                Text = x.ProductName,
                                                                Value = x.ProductID
                                                            })
                        };
       return View(model);
    }

However it complains that Value = x.Product cant convert type int to string. So if I add a .ToString it compiles ok but when I try load the view I get an error

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

My View

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>CreatePurchaseViewModel</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.SelectedProductId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(x => x.SelectedProductId,Model.Products)
        @Html.ValidationMessageFor(model => model.SelectedProductId)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.OrderDate)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model=>model.OrderDate)
        @Html.ValidationMessageFor(model => model.OrderDate)
    </div>
    <div>
    Sent
    @Html.RadioButtonFor(model => model.OrderSent, "Sent", new { @checked = true })
    Not Sent
    @Html.RadioButtonFor(model=>model.OrderSent,"Not Sent")

Im pretty new to both entity framework and mvc so any help would be great.

Thank you


You haven't specified how does your Product model look like but we can assume that the ProductID property is integer so you might need to convert it to a string:

[HttpGet]
public ActionResult Create()
{
    var model = new CreatePurchaseViewModel
    {
        Products = context.Products.Select(x =>
            new SelectListItem
            {
                Text = x.ProductName,
                Value = x.ProductID.ToString()
            }
        )
   };
   return View(model);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消