Moving Items from One List To Another using MVC


MODEL CLASS

Add DbContext and Entity Class

DbContext.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Demo1.Models
{
    public class DbContext
    {
         public int EmpID { get; set; }
        public string EmpName { get; set; }

        public DbContext(int Empid, string EmpName)
        {
            this.EmpID = Empid;
            this.EmpName = EmpName;

        }

        //sample list to use in place of database
        static List<DbContext> Employees = new List<DbContext>
        {
          
                new DbContext(1001,"Salman"),
                new DbContext(1002,"Amit"),
                new DbContext(1003,"Rajeev"),
                new DbContext(1004,"Ameer"),
                new DbContext(1005,"Sanjay"),
                new DbContext(1006,"Rahul"),
                new DbContext(1007,"Mangal"),
                new DbContext(1008,"Sandeep"),
                new DbContext(1009,"Mukesh")
        };

        public static List<DbContext> GetEmployees()
        {
            return Employees.OrderBy(o=>o.EmpName).ToList();
        }

    }
}



EmpLogic

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo1.Models
{
    public class EmpLogic
    {
        public List<DbContext> AvailableEmployees { get; set; }
        public List<DbContext> RequestedEmployees { get; set; }
        public int[] AvailableSelected { get; set; }
        public int[] RequestedSelected { get; set; }
        public string SavedRequested { get; set; }
    }
}


HomeController


using Demo1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Demo1.Controllers
{
    public class HomeController : Controller
    {

     [HttpGet]
        public ActionResult Index()
        {
            EmpLogic modell = new EmpLogic { AvailableEmployees = DbContext.GetEmployees(), RequestedEmployees = new List<DbContext>() };
            return View(modell);
        }

        [HttpPost]
        public ActionResult Index(EmpLogic model, string add, string remove, string send)
        {
            //Need to clear model state or it will interfere with the updated model
            ModelState.Clear();
            RestoreSavedState(model);
            if (!string.IsNullOrEmpty(add))
                AddProducts(model);
            else if (!string.IsNullOrEmpty(remove))
                RemoveProducts(model);
           
            SaveState(model);
            return View(model);
        }
        void SaveState(EmpLogic model)
        {
            //create comma delimited list of product ids
            model.SavedRequested = string.Join(",", model.RequestedEmployees.Select(p => p.EmpID.ToString()).ToArray());

            //Available products = All - Requested
            model.AvailableEmployees = DbContext.GetEmployees().Except(model.RequestedEmployees).ToList();
        }

        void RemoveProducts(EmpLogic model)
        {
            if (model.RequestedSelected != null)
            {
                model.RequestedEmployees.RemoveAll(p => model.RequestedSelected.Contains(p.EmpID));
                model.RequestedSelected = null;
            }
        }

        void AddProducts(EmpLogic model)
        {
            if (model.AvailableSelected != null)
            {
                var prods = DbContext.GetEmployees().Where(p => model.AvailableSelected.Contains(p.EmpID));
                model.RequestedEmployees.AddRange(prods);
                model.AvailableSelected = null;
            }
        }

        void RestoreSavedState(EmpLogic model)
        {
            model.RequestedEmployees = new List<DbContext>();

            //get the previously stored items
            if (!string.IsNullOrEmpty(model.SavedRequested))
            {
                string[] prodids = model.SavedRequested.Split(',');
                var prods = DbContext.GetEmployees().Where(p => prodids.Contains(p.EmpID.ToString()));
                model.RequestedEmployees.AddRange(prods);
            }
        }
    }
}


INDEX.CSHTML

@model  Demo1.Models.EmpLogic

@using (Html.BeginForm())
{
    <div>
        <table>
            <thead>
                <tr>
                    <th >Available Employee</th>
                    <th>&nbsp;</th>
                    <th>Requested Employee</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td valign="top">
                        @Html.ListBoxFor(model => model.AvailableSelected,
      new MultiSelectList(Model.AvailableEmployees, "EmpID", "EmpName", Model.AvailableSelected), new { @style = "width:150px;height:300px;" })
                    </td>
                    <td valign="top">
                        <input type="submit" name="add" id="add" value=">>" /><br />
                        <input type="submit" name="remove" id="remove" value="<<" />
                    </td>
                    <td valign="top">
                        @Html.ListBoxFor(model => model.RequestedSelected, new MultiSelectList(Model.RequestedEmployees, "EmpId", "EmpName", Model.RequestedSelected), new { @style = "width:150px;height:300px;" })
                    </td>
                </tr>
            </tbody>
        </table>
        <br />
    @Html.HiddenFor(model=>model.SavedRequested)

    

    </div>
}



OUTPUT



Share this

Previous
Next Post »