title: Entity framework does not save my data to sql server
body: <p>I have code below. It is using Entity Framework(5.0) to handle data in/out to sql server(2008). Unfortunately, the modified data Can Not be successful save to database. Any body help me out will be great appreciate.</p>
  
  <hr>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  using System.Web.Mvc;
  
  namespace MvcApplication1.Models
  {
      public class ProductEditViewModel
      {
          NorthwindEntities northwind = new NorthwindEntities();
  
          public Product Product;
  
          public ProductEditViewModel(Int32 ProductId)
          {
              Product = (from pro in northwind.Products
                         where pro.ProductID == ProductId
                         select pro).FirstOrDefault();
          }
  
          // For DropDownListFor need IEnumerable&lt;SelectListItem&gt;
          public IEnumerable&lt;SelectListItem&gt; SupplierItems
          {
              get
              {
                  var q = from sup in northwind.Suppliers.AsEnumerable()
                          select new SelectListItem
                              {
                                  Text = sup.CompanyName,
                                  Value = sup.SupplierID.ToString(),
                                  Selected = sup.SupplierID == Product.SupplierID
                              };
  
                  return q;
              }
          }
  
          // For RadioButtonFor need below
          public IEnumerable&lt;Category&gt; CategorieItems
          {
              get
              {
                  var q = from cat in northwind.Categories.AsEnumerable() select cat;
                  return q;
              }
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>[HttpGet]
  public ActionResult ProductEdit(Int32 ProductId)
  {
      var vm = new ProductEditViewModel(ProductId);
      return View(vm);
  }
  
  [HttpPost]
  public ActionResult ProductEdit(Int32 ProductId, FormCollection fc)
  {
      var vm = new ProductEditViewModel(ProductId);
      UpdateModel(vm.Product, "Product");
  
      var northwind = new NorthwindEntities();
      northwind.SaveChanges();
  
      return View(vm);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@model MvcApplication1.Models.ProductEditViewModel
  
  @{
      Layout = null;
  }
  
  &lt;script type="text/javascript"&gt;
  
  &lt;/script&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;ProductEdit&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;script src="~/Scripts/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
      &lt;script src="~/Scripts/jquery.validate.min.js"&gt;&lt;/script&gt;
      &lt;script src="~/Scripts/jquery.validate.unobtrusive.min.js"&gt;&lt;/script&gt;
  
  
      @using (Html.BeginForm())
      {
          @Html.AntiForgeryToken()
  
          &lt;div class="form-horizontal"&gt;
              &lt;h4&gt;Product&lt;/h4&gt;
              &lt;hr /&gt;
              @Html.ValidationSummary(true, "", new { @class = "text-danger" })
              @Html.HiddenFor(model =&gt; model.Product.ProductID)
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Product.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.Product.ProductName, new { htmlAttributes = new { @class = "form-control" } })
                      @Html.ValidationMessageFor(model =&gt; model.Product.ProductName, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Product.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @*DropDownList is for deiplay purpose*@
                      @*@Html.DropDownList("SupplierID", Model.SupplierItems, htmlAttributes: new { @class = "form-control" })*@
  
                      @*DropDownListFor is for Edit purpose*@
                      @Html.DropDownListFor(model =&gt; model.Product.SupplierID, Model.SupplierItems, htmlAttributes: new { @class = "form-control" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Product.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @*RadioButton is for Display purpose*@
                      @*@foreach (var Categorie in Model.CategorieItems)
                          {
                              @Html.RadioButton("CategoryID", Categorie.CategoryID, Model.CategorySelectedId == Categorie.CategoryID) @Categorie.CategoryName; @:&amp;nbsp;&amp;nbsp;&amp;nbsp;
                          }*@
  
                      @*RadioButtonFor is for Edit purpose*@
                      @foreach (var Categorie in Model.CategorieItems)
                      {
                          @Html.RadioButtonFor(model =&gt; model.Product.CategoryID, Categorie.CategoryID) @Categorie.CategoryName; @:&amp;nbsp;&amp;nbsp;&amp;nbsp;
                      }
  
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Product.UnitPrice, htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.EditorFor(model =&gt; model.Product.UnitPrice, new { htmlAttributes = new { @class = "form-control" } })
                      @Html.ValidationMessageFor(model =&gt; model.Product.UnitPrice, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.Product.Discontinued, htmlAttributes: new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      &lt;div class="checkbox"&gt;
                          @Html.EditorFor(model =&gt; model.Product.Discontinued)
                          @Html.ValidationMessageFor(model =&gt; model.Product.Discontinued, "", new { @class = "text-danger" })
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;div class="col-md-offset-2 col-md-10"&gt;
                      &lt;input type="submit" value="Save" class="btn btn-default" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      }
  
      &lt;div&gt;
          @Html.ActionLink("Back to List", "Index")
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>