Click here to Skip to main content
15,887,416 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
@model IEnumerable<BulkDelete.Models.Employee>

@{int[] employeesToUpdate;}


    <div style="font-family:Arial">
        @using (Html.BeginForm("UpdateMultiple", "Home", FormMethod.Post))
        {
            <table class="table">
                <thead>
                    <tr>
                        <td>Checkbox<br /></td>

                        <th>Name</th>
                        <th>Gender</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>

                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                <input type="checkbox" name="employeesToUpdate" id="employeesToUpdate" value="@item.ID" />
                                
                            </td>
                            <td>@item.Name</td>
                            <td>@item.Gender</td>
                            <td>@item.Email</td>
                        </tr>
                    }

                </tbody>
               

            </table>

            <input type="submit"  value="update selected employees" />


        }
    </div>



The CONTROLLER:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BulkDelete.Models;
namespace BulkDelete.Controllers
{
    public class HomeController : Controller
    {
        SampleDBContext db = new SampleDBContext();
        public  System.Web.SessionState.HttpSessionState Session { get; }

        public ActionResult Index()
        {
            return View(db.Employees.ToList()) ;
        }


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

        
        //[HttpPost]
        public ActionResult UpdateMultipleRecords()
        {

            IEnumerable<int> employeesToUpdate = (IEnumerable<int>)TempData["employeesToUpdate"];
            var listemployee = db.Employees.Where(x => employeesToUpdate.Contains(x.ID));
            foreach (var item in listemployee)
            {
                int itemid = item.ID;
                Employee e = db.Employees.Find(itemid);
                e.Email = "hello.";
               // e = db.Employees.Find(item);
                db.Entry(e).State = EntityState.Modified;
                
            }
            db.SaveChanges();
            return RedirectToAction("Index");
        }
}



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

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.
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.

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.


Line 34:             IEnumerable<int> employeesToUpdate = (IEnumerable<int>)TempData["employeesToUpdate"];
Line 35:             var listemployee = db.Employees.Where(x => employeesToUpdate.Contains(x.ID));
Line 36:             foreach (var item in listemployee)
Line 37:             {
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");
}
 
Share this answer
 
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