如何在asp.net mvc4中对提交按钮执行特定操作

时间:2014-05-05 07:17:43

标签: c# jquery asp.net asp.net-mvc asp.net-mvc-4

我正在使用Dual ListBoxes并使用两个按钮将数据从一个列表框移动到另一个列表框..

以下代码我将数据从一个列表框移动到另一个列表框

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; }
    }

//控制器

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

查看

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)) %>

但问题是,当我点击添加(&gt;&gt;)或删除(&lt;&lt;&lt;)按钮时,操作会在整个页面上执行,就像提交按钮一样,该按钮会保存该页面中的数据到db。在这里,我想知道在点击添加(&gt;&gt;)或删除(&lt;&lt;&lt;)按钮后如何执行操作按钮。

请帮助解决这个问题

1 个答案:

答案 0 :(得分:1)

这个想法是一个表单可以包含多个提交按钮,以不同的方式发布表单帖子。

enter image description here