SBSB
SBSB

Reputation: 23

how to perform specific action on submit button in asp.net mvc4

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

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

public string InstituteName { get; set; }
    }

MemberAccessRights.cs

    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

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

Upvotes: 1

Views: 374

Answers (1)

Kumar Manish
Kumar Manish

Reputation: 3772

The idea was that a form could contain more than one submit button issuing a form post to a different way.

enter image description here

Upvotes: 1

Related Questions