title: MVC gives Error Value cannot be null or empty. when i return model to view during updateing
body: <p>I am trying to return model to view to <strong>Preserve</strong> the data that the user entered if there is an exception. So when I return the model to view it tells me "Value cannot be null or empty" but the model still has the data</p>
  
  <pre><code>@Html.EditorFor(model =&gt; model.NewsTitle, new { htmlAttributes = new { @class = "form-control" } })
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/X73ws.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X73ws.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/it9jd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/it9jd.jpg" alt="enter image description here"></a></p>
  
  <pre><code>    [HttpGet]
      public ActionResult Edit(int? Id)
      {
          using (var db = new NewsDatabaseEntities())
          {
              db.Configuration.LazyLoadingEnabled = false;
  
              var Result = (from n in db.News.Include("Categories")
                            from c in db.Categories
                            where n.NewsId == Id
                            select new { news = n, neweCategories = n.Categories, cate = c }).ToList();
  
  
              News NewsDetails = (from news in Result
                                  select new News
                                  {
                                      NewsId = news.news.NewsId,
                                      NewsTitle = news.news.NewsTitle,
                                      NewsBody = news.news.NewsBody,
                                      NewsImagePath = news.news.NewsImagePath,
                                      Categories = news.neweCategories
                                  }).FirstOrDefault();
  
              var AllCategories = (from c in Result
                                   select new Category
                                   {
                                       CategoryId = c.cate.CategoryId,
                                       CategoryName = c.cate.CategoryName
                                   }).ToList();
  
  
  
              if (NewsDetails != null)
              {
  
                  var model = new NewsViewModel();
                  model.NewsId = NewsDetails.NewsId;
                  model.AllCategories = AllCategories;
                  model.Categories = NewsDetails.Categories;
                  model.NewsTitle = NewsDetails.NewsTitle;
                  model.NewsBody = NewsDetails.NewsBody;
                  model.NewsImagePath = NewsDetails.NewsImagePath;
                  return View(model);
              }
              else
              {
                  return RedirectToAction("Index", "Home");
              }
          }
          return View();
      }
  
  
  
  
  
      [ValidateInput(false)]
      [ValidateAntiForgeryToken]
      public ActionResult Edit(NewsViewModel model)
      {
  
  
          using (var db = new NewsDatabaseEntities())
          {
              db.Configuration.LazyLoadingEnabled = false;
  
              News NewsToUpdate = db.News.Include("Categories").SingleOrDefault(n =&gt; n.NewsId == model.NewsId);
  
  
              if (NewsToUpdate != null)
              {
                  using (var transaction = db.Database.BeginTransaction())
                  {
  
                      try
                      {
  
                          db.SaveChanges();
  
                          transaction.Commit();
  
                          return RedirectToAction("Details", "Admin", new { Id = model.NewsId });
  
                      }
  
                      catch (Exception ex)
                      {
  
                          transaction.Rollback();
  
                          TempData["Error"] = ex.Message + " " + ex.ToString();
  
  
                          return View(model);
  
                      }
  
                  }
  
              }
              else
              {
                  return RedirectToAction("Index", "Home");
  
              }
  
          }
  
          return View();
      }
  </code></pre>
  
  <p>Razor View</p>
  
  <pre><code>@model Assignment.Models.NewsViewModel
  
  @{
      ViewBag.Title = "Edit";
  }
  
  &lt;h2&gt;Update News&lt;/h2&gt;
  &lt;br /&gt;
  
  
  @if (TempData["Error"] != null)
  {
      &lt;p class=" alert alert-danger"&gt;
          &lt;strong&gt;Error!  &lt;/strong&gt;
  
          @TempData["Error"]
  
      &lt;/p&gt;
  }
  
  @using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { id = "MyForm", enctype = "multipart/form-data" }))
  {
  
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
  
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
  
          @Html.HiddenFor(model =&gt; model.NewsId)
          @Html.HiddenFor(model =&gt; model.SelectedCategoriesIds, new { id = "hid" })
  
  
          &lt;div class="form-group"&gt;
              @Html.Label("Enter News Title", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.NewsTitle, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.NewsTitle, "", new { @class = "text-danger" })
  
  
                      &lt;br /&gt;&lt;br /&gt;
                      &lt;img src="@Url.Content(Model.NewsImagePath)" alt="Image" id="ImagePreview" width=330 height=250 /&gt;
                  &lt;br /&gt;&lt;br /&gt;
  
                      @Html.TextBoxFor(model =&gt; model.NewsImageFile, new { type = "file", data_val = "false" })
                  @Html.ValidationMessageFor(model =&gt; model.NewsImageFile, "", new { @class = "text-danger" })
  
  
                  &lt;br /&gt; &lt;br /&gt;
  
  
                      &lt;div class="form-group"&gt;
  
                          &lt;div class="col-md-10"&gt;
  
  
  
                              @Html.TextAreaFor(model =&gt; model.NewsBody, new { htmlAttributes = new { @class = "ckeditor" } })
                              @Html.ValidationMessageFor(model =&gt; model.NewsBody, "", new { @class = "text-danger" })
                              &lt;br /&gt;
                              This News under these categories: &lt;br /&gt;&lt;br /&gt;
  
  
                              &lt;select id="dropdownOne" name="dropdownOne" multiple="multiple" class="form-control"&gt;
  
  
                                  @foreach (var Category in Model.AllCategories)
                              {
  
  
  
                                  if (Model.Categories.Select(c =&gt; c.CategoryId).Contains(Category.CategoryId))
                                  {
                                      &lt;option selected value="@Category.CategoryId"&gt;@Category.CategoryName&lt;/option&gt;
                                  }
                                  else
                                  {
                                      &lt;option value="@Category.CategoryId"&gt;@Category.CategoryName&lt;/option&gt;
                                  }
                              }
  
  
                              &lt;/select&gt;
  
                          &lt;/div&gt;
                      &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;
  
  
  }
  </code></pre>