2014-05-05 25 views
1

我與雙列表框工作,並使用兩個按鈕來從一個列表框的數據移動到另一個提交按鈕的具體行動..如何執行關於在asp.net mvc4

這裏下面的代碼我已經做了將數據從移動一個列表框到另一個

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

但存在的問題是,當我點擊添加(>>)或刪除(< <)按鈕的動作是在整個頁面上執行的,就像提交按鈕,將數據從該頁面保存到數據庫。在這裏我想知道如何在點擊添加(>>)或刪除(< <)按鈕後執行操作按鈕。

請幫忙解決這個

+0

看到此鏈接:http://stackoverflow.com/questions/18284977/asp-net-mvc-multiple-submit-buttons-using-ajax-beginform – Jaimin

+0

使用引導duallistbox HTTP:// WWW .virtuosoft.eu/code/bootstrap-duallistbox/ –

回答

1

當時的想法是一種形式可以包含多個提交按鈕發出表單提交到一個不同的方式。

enter image description here

+0

你可以在單個視圖中使用多個html.beginform –

+0

此鏈接更多幫助代碼:http://stackoverflow.com/questions/442704/how-do-you-處理多個提交按鈕在asp-net-mvc框架 –

+0

好吧,我會盡力...,謝謝你的幫助 – SBSB