Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with Dual ListBoxes and using two buttons to move data from one listbox to another..

Here following code I have done to move data from one Listbox to another

InstituteInformation.cs


C#
public class InstituteInformation
    {
public int Id { get; set; }

public string InstituteName { get; set; }
    }



MemberAccessRights.cs

XML
public class MemberAccessRights
    {
public int Id { get; set; }

public List<InstituteInformation> AvailableNames { get; set; }
public int[] AvailableSelected { get; set; }


public List<InstituteInformation> RequestedNames { get; set; }
public string[] RequestedSelected { get; set; }

public string SavedRequested { get; set; }
    }


Controller
//
    // GET: /MemberDetails/Create

     public ActionResult Create()
        {
Wrapper1 MD = new Wrapper1();
            MD.MAR = new MemberAccessRights{ AvailableNames = getAllInstituteNameList(), RequestedNames = new List<InstituteInformation>() };
return View(MD);
        }

//
// POST: /MemberDetails/Create

        [HttpPost]
    public ActionResult Create(Wrapper1 MD, string add, string remove)
        {
try
            {
ModelState.Clear();
RestoreSavedState(MD);
if (!string.IsNullOrEmpty(add))
AddNames(MD);
elseif (!string.IsNullOrEmpty(remove))
AddNames(MD);
SaveState(MD);

    using (varMDConext = new WrapperDB())
                {
MDConext.MBIDBS.Add(MD.MBI);
MDConext.MACDBS.Add(MD.MAC);
MDConext.MARDBS.Add(MD.MAR);
MDConext.MODBS.Add(MD.MO);
                }

returnRedirectToAction("Index");
            }
catch
            {
return View(MD);
            }
        }
        #regionSupportFuncs

void SaveState(Wrapper1 MD)
        {
MD.MAR.SavedRequested = string.Join(",", MD.MAR.RequestedNames.Select(p =>p.Id.ToString()).ToArray());

////Available Names = All - Requested
MD.MAR.AvailableNames = getAllInstituteNameList().Except(MD.MAR.RequestedNames).ToList();

        }

//RestoreSavedState
void RestoreSavedState(Wrapper1 MD)
        {
MD.MAR.RequestedNames = newList<InstituteInformation>();

if (!string.IsNullOrEmpty(MD.MAR.SavedRequested))
            {
string[] nameids = MD.MAR.SavedRequested.Split(',');
var name = getAllInstituteNameList().Where(p =>nameids.Contains(p.Id.ToString()));
MD.MAR.RequestedNames.AddRange(name);
            }
        }

//AddNames
void AddNames(Wrapper1 MD)
        {
if (MD.MAR.AvailableSelected != null)
            {
var names = getAllInstituteNameList().Where(p =>MD.MAR.AvailableSelected.Contains(p.Id));
MD.MAR.RequestedNames.AddRange(names);
MD.MAR.AvailableSelected = null;
            }
        }

//RemoveNames
void RemoveNames(Wrapper1 MD)
        {
if (MD.MAR.RequestedSelected != null)
            {
MD.MAR.RequestedNames.RemoveAll(p =>MD.MAR.RequestedSelected.Contains(p.Id.ToString()));
MD.MAR.RequestedSelected = null;
            }
        }
        #endregion


View


XML
List of Financial Institute

     <%:Html.ListBoxFor(model=>model.MAR.AvailableSelected,new MultiSelectList(Model.MAR.AvailableNames,"Id","InstituteName",Model.MAR.AvailableSelected)) %>

     <div>

     <input id="add" name="add" type="submit" value=">>" />
     <br />
     <input id="remove" name="remove" type="submit" value="<<" />

     </div>


    <%:Html.ListBoxFor(m=>m.MAR.RequestedSelected,new MultiSelectList(Model.MAR.RequestedNames,"Id","Name",Model.MAR.RequestedSelected)) %>




But there is the problem is that when I click on add(>>) or remove(<<) button the action is performed on the complete page just like the submit button which save the data from that page to db. Here I wanted to know how to perform the action button after clicking the add(>>) or remove(<<) button.

please help to solve this
Posted
Updated 4-May-14 21:31pm
v2
Comments
Sampath Lokuge 5-May-14 3:57am    
Do you need to know about how to do the partial page update or what ?
Member 10714619 5-May-14 4:49am    
yes

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