title: Values are null when I wanna edit DB in ASP.NET MVC
body: <p>I have a problem with my project in ASP.NET Model-View-Controller. </p>
  
  <p>I have an application "Guestbook". I can successfully create guests, but I cannot edit them.</p>
  
  <p>Look: 
  1. I start application - all ok<br>
  2. I create a guest - all ok (guest has all the entered data)<br>
  3. I click "edit" - all ok<br>
  4. I see new window. All fields on the screen have correct data, but I have a breakpoint in edit function and I see that all variables are null.      </p>
  
  <p>Model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data.Entity;
  using System.ComponentModel.DataAnnotations;
  
  namespace Guestbook.Models
  {
      public class Guest
      {
          public int ID { get; set; }
  
          [Display(Name = "name", ResourceType=typeof(Resources.Resources))]
          [Required(ErrorMessageResourceType = typeof(Resources.Resources),
                ErrorMessageResourceName = "nameRequired")]
          [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
                            ErrorMessageResourceName = "nameLong")]
          public string name { get; set; }
  
          [Display(Name = "surname", ResourceType = typeof(Resources.Resources))]
          [Required(ErrorMessageResourceType = typeof(Resources.Resources),
                ErrorMessageResourceName = "surnameRequired")]
          [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
                            ErrorMessageResourceName = "surnameLong")]
          public string surname { get; set; }
  
          [Display(Name = "firm", ResourceType = typeof(Resources.Resources))]
          [Required(ErrorMessageResourceType = typeof(Resources.Resources),
                ErrorMessageResourceName = "firmRequired")]
          [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
                            ErrorMessageResourceName = "firmLong")]
          public string firm { get; set; }
  
          [Display(Name = "toWhom", ResourceType = typeof(Resources.Resources))]
          [Required(ErrorMessageResourceType = typeof(Resources.Resources),
                ErrorMessageResourceName = "toWhomRequired")]
          [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
                            ErrorMessageResourceName = "toWhomLong")]
          public string toWhom { get; set; }
  
          [Display(Name = "permitNumber", ResourceType = typeof(Resources.Resources))]
          [Required(ErrorMessageResourceType = typeof(Resources.Resources),
                    ErrorMessageResourceName = "permitRequired")]
          [Range(0, 10000, ErrorMessageResourceType = typeof(Resources.Resources),
                         ErrorMessageResourceName = "permitRange")]
          public int permitNumber { get; set; }
  
          [Display(Name = "magazine", ResourceType = typeof(Resources.Resources))]
          [Required(ErrorMessageResourceType = typeof(Resources.Resources),
                ErrorMessageResourceName = "magazineRequired")]
          public bool magazine { get; set; } 
  
  
          [Display(Name = "entranceTime", ResourceType = typeof(Resources.Resources))]
          [DataType(DataType.Date)]
          public DateTime? entranceTime { get; set; }
  
  
          [Display(Name = "entranceTimeTime", ResourceType = typeof(Resources.Resources))]
          public string entranceTimeTime { get; set; }
  
  
          [Display(Name = "exitDate", ResourceType = typeof(Resources.Resources))]
          [DataType(DataType.Date)]
          public DateTime? exitDate { get; set; }
  
  
          [Display(Name = "exitTime", ResourceType = typeof(Resources.Resources))]
          public string exitTime { get; set; }
      }
  
      public class GuestDBContext : DbContext
      {
          public DbSet&lt;Guest&gt; Guests { get; set; }
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Net;
  using System.Web;
  using System.Web.Mvc;
  using Guestbook.Models;
  
  namespace Guestbook.Controllers
  {
      public class GuestsController : BaseController
      {
          private GuestDBContext db = new GuestDBContext();
  
          // GET: Guests
          public ActionResult Index()
          {
              //Sortowanie po nazwisku.
              var guests = from s in db.Guests select s;
              guests = guests.OrderBy(s =&gt; s.surname);
              return View(guests.ToList());
          }
  
          // GET: Guests/Details/5
          public ActionResult Details(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              Guest guest = db.Guests.Find(id);
              if (guest == null)
              {
                  return HttpNotFound();
              }
              return View(guest);
          }
  
          // GET: Guests/Create
          public ActionResult Create()
          {
              return View();
          }
  
          // POST: Guests/Create
          // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
          // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create([Bind(Include = "ID,name,surname,firm,toWhom,permitNumber,magazine,entranceTime,entranceTimeTime,exitDate,exitTime")] Guest guest)
          {
              guest.entranceTime = DateTime.Today;
              guest.exitDate = DateTime.Today;
              DateTime dt = DateTime.Now;
              string format = "HH:mm:ss";
              guest.entranceTimeTime = dt.ToString(format);
              guest.exitTime = dt.ToString(format);
              if (ModelState.IsValid)
              {
                  db.Guests.Add(guest);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              return View(guest);
          }
  
          // GET: Guests/Edit/5
          public ActionResult Edit(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              Guest guest = db.Guests.Find(id);
              if (guest == null)
              {
                  return HttpNotFound();
              }
              return View(guest);
          }
  
          // POST: Guests/Edit/5
          // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
          // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Edit([Bind(Include = "exitDate,exitTime")] Guest g)
          {
  
              g.exitDate = DateTime.Today;
              DateTime dt = DateTime.Now;
              string format = "HH:mm:ss";
              g.exitTime = dt.ToString(format);
              if (ModelState.IsValid)
              {
                  db.Entry(g).State = EntityState.Modified;
                  db.Entry(g).Property(gg =&gt; gg.name).IsModified = false;
                  db.Entry(g).Property(gg =&gt; gg.surname).IsModified = false;
                  db.Entry(g).Property(gg =&gt; gg.firm).IsModified = false;
                  db.Entry(g).Property(gg =&gt; gg.permitNumber).IsModified = false;
                  db.Entry(g).Property(gg =&gt; gg.magazine).IsModified = false;
                  db.Entry(g).Property(gg =&gt; gg.toWhom).IsModified = false;
                  db.Entry(g).Property(gg =&gt; gg.entranceTime).IsModified = false;
                  db.Entry(g).Property(gg =&gt; gg.entranceTimeTime).IsModified = false;
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
              return View(g);
          }
  
          // GET: Guests/Delete/5
          public ActionResult Delete(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              Guest guest = db.Guests.Find(id);
              if (guest == null)
              {
                  return HttpNotFound();
              }
              return View(guest);
          }
  
          // POST: Guests/Delete/5
          [HttpPost, ActionName("Delete")]
          [ValidateAntiForgeryToken]
          public ActionResult DeleteConfirmed(int id)
          {
              Guest guest = db.Guests.Find(id);
              db.Guests.Remove(guest);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          protected override void Dispose(bool disposing)
          {
              if (disposing)
              {
                  db.Dispose();
              }
              base.Dispose(disposing);
          }
  
      }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model Guestbook.Models.Guest
  
  @{
      ViewBag.Title = @Resources.exitGuest;
  }
  
  &lt;h2&gt;@Resources.exitGuest&lt;/h2&gt;
  
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
  
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          @Html.HiddenFor(model =&gt; model.ID)
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.name, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DisplayFor(model =&gt; model.name, new { htmlAttributes = new { @class = "form-control" } })
  
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.surname, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DisplayFor(model =&gt; model.surname, new { htmlAttributes = new { @class = "form-control" } })
  
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.firm, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DisplayFor(model =&gt; model.firm, new { htmlAttributes = new { @class = "form-control" } })
  
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;br /&gt;
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;b&gt;@Resources.leftOk&lt;/b&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=@Resources.y class="btn btn-default" /&gt;
                  &lt;input type="button" value=@Resources.cancel onclick="location.href='@Url.Action("","guests")'" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>