Click here to Skip to main content
14,845,586 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In the code below I have created an index view that enables users to check records that they want to update. When clicking on the submit button, they are supposed to be redirected to a webpage, and some code needs to be executed to update these records.

The following is the code that I have implemented:

The VIEW
  1  @model IEnumerable<BulkDelete.Models.Employee>
  2  
  3  @{int[] employeesToUpdate;}
  4  
  5  
  6      <div style="font-family:Arial">
  7          @using (Html.BeginForm("UpdateMultiple", "Home", FormMethod.Post))
  8          {
  9              <table class="table">
 10                  <thead>
 11                      <tr>
 12                          <td>Checkbox<br /></td>
 13  
 14                          <th>Name</th>
 15                          <th>Gender</th>
 16                          <th>Email</th>
 17                      </tr>
 18                  </thead>
 19                  <tbody>
 20  
 21                      @foreach (var item in Model)
 22                      {
 23                          <tr>
 24                              <td>
 25                                  <input type="checkbox" name="employeesToUpdate" id="employeesToUpdate" value="@item.ID" />
 26                                  
 27                              </td>
 28                              <td>@item.Name</td>
 29                              <td>@item.Gender</td>
 30                              <td>@item.Email</td>
 31                          </tr>
 32                      }
 33  
 34                  </tbody>
 35                 
 36  
 37              </table>
 38  
 39              <input type="submit"  value="update selected employees" />
 40  
 41  
 42          }
 43      </div>



The CONTROLLER:

  1  using System;
  2  using System.Collections.Generic;
  3  using System.Data.Entity;
  4  using System.Data.Entity.Core.Objects;
  5  using System.Linq;
  6  using System.Web;
  7  using System.Web.Mvc;
  8  using BulkDelete.Models;
  9  namespace BulkDelete.Controllers
 10  {
 11      public class HomeController : Controller
 12      {
 13          SampleDBContext db = new SampleDBContext();
 14          public  System.Web.SessionState.HttpSessionState Session { get; }
 15  
 16          public ActionResult Index()
 17          {
 18              return View(db.Employees.ToList()) ;
 19          }
 20  
 21  
 22          [HttpPost]
 23          public ActionResult UpdateMultiple(IEnumerable<int> employeesToUpdate)
 24          {
 25              
 26              return RedirectToAction("UpdateMultipleRecords");
 27          }
 28  
 29          
 30          //[HttpPost]
 31          public ActionResult UpdateMultipleRecords()
 32          {
 33  
 34              IEnumerable<int> employeesToUpdate = (IEnumerable<int>)TempData["employeesToUpdate"];
 35              var listemployee = db.Employees.Where(x => employeesToUpdate.Contains(x.ID));
 36              foreach (var item in listemployee)
 37              {
 38                  int itemid = item.ID;
 39                  Employee e = db.Employees.Find(itemid);
 40                  e.Email = "hello.";
 41                 // e = db.Employees.Find(item);
 42                  db.Entry(e).State = EntityState.Modified;
 43                  
 44              }
 45              db.SaveChanges();
 46              return RedirectToAction("Index");
 47          }
 48  }



I keep getting the same error over and over again which is :

  1  Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only entity types, enumeration types or primitive types are supported in this context.
  2  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  3  
  4  Exception Details: System.NotSupportedException: Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only entity types, enumeration types or primitive types are supported in this context.
  5  
  6  
  7  Line 34:             IEnumerable<int> employeesToUpdate = (IEnumerable<int>)TempData["employeesToUpdate"];
  8  Line 35:             var listemployee = db.Employees.Where(x => employeesToUpdate.Contains(x.ID));
  9  Line 36:             foreach (var item in listemployee)
 10  Line 37:             {
 11  Line 38:                 int itemid = item.ID;


What I have tried:

Code works when it is simply executed from the index page, but when redirecting to another web page this error keeps showing up
Posted
Updated 17-Sep-20 2:21am
Comments
F-ES Sitecore 17-Sep-20 7:59am
   
Google how to pass a list or array to a controller, you can't use "foreach(var x in Model)" in the view, you have to use a "for" loop and array indexing, but the samples you find will show you what you need to do.

1 solution

Quote:
C#
[HttpPost]
public ActionResult UpdateMultiple(IEnumerable<int> employeesToUpdate)
{
    return RedirectToAction("UpdateMultipleRecords");
}

public ActionResult UpdateMultipleRecords()
{
    IEnumerable<int> employeesToUpdate = (IEnumerable<int>)TempData["employeesToUpdate"];
You haven't stored the list in the TempData collection in your UpdateMultiple action, so the employeesToUpdate variable will be null in your UpdateMultipleRecords action.
C#
public ActionResult UpdateMultiple(IEnumerable<int> employeesToUpdate)
{
    TempData["employeesToUpdate"] = employeesToUpdate;
    return RedirectToAction("UpdateMultipleRecords");
}
   
Comments
Member 13029506 17-Sep-20 11:15am
   
Thanks, worked perfectly :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900