2016-02-19 70 views
0

我有一個包含我使用的下拉列表視圖中的信息視圖模型:asp.net mvc5 - 傳遞選擇的列表項的ID回控制器

public class AddPlayersToGame 
{ 
    public string GameTitle { set; get; } 
    public int GameID { set; get; } 
    public List<SelectListItem> Players { set; get; } 
    public int PlayerID { get; set; } 
    public int[] SelectedPlayers { set; get; } 
} 

這是我的看法,其簡單地顯示包含運動員列表以選擇從下拉列表:

@model WebGameProj.ViewModels.AddPlayersToGame 
<div> 
{ 
    @Html.DropDownListFor(x => Model.PlayerID, Model.Players) 

    <input type="submit" /> 
} 
</div> 

這是控制器的方法我使用:

public ActionResult AddPlayersView(int id) 
{ 
    var GameSelected = db.Games.Find(id); 

    if (GameSelected== null) 
    { 
     return HttpNotFound(); 
    } 

    var np = new AddPlayersToGame { GameID = id, GameTitle = GameSelected.GameTitle }; 

    np.Players = db.Players.Select(m => new SelectListItem 
    { 
     Text = m.PlayerUserName, 
     Value = m.PlayerId.ToString() 
    }).ToList(); 

    return View(np); 
} 

[HttpPost] 
public ActionResult AddPlayersView(AddPlayersToGame model) 
{ 
    foreach (var item in model.SelectedPlayers) 
    { 
     var SelPlayer = db.Players.Find(model.PlayerID); 

     if (SelPlayer== null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 

     if (SelPlayer != null) 
     { 
      Game GameSelected = new Game(); 
      GameSelected.GamePlayers.Add(SelPlayer); 

      db.Entry(GameSelected).State = EntityState.Modified; 
      db.SaveChanges(); 
     } 
    } 
    return RedirectToAction("GameDetailsView"); 
} 

所以,基本上我想有一個顯示玩家下拉列表的視圖,當某些玩家被選中後,post方法將通過使用他們的id通過下拉菜單返回來找到數據庫上的每個玩家列表中,然後將它們添加到該遊戲的當前玩家列表中。

+0

您可以使用jQuery用於獲取所選的ID。 –

+0

你嘗試過搜索類似的東西嗎?說是否[這](http://stackoverflow.com/questions/25013223/asp-net-mvc-multiple-select-dropdown)幫助? – Andrei

+1

您只能在下拉列表中一次選擇一個項目。您需要使用多選下拉列表(jQuery UI具有多選列表)才能達到此目的。 – ataravati

回答

1

模型更改爲

public class AddPlayersToGame 
{ 
    public string GameTitle { set; get; } 
    public int GameID { set; get; } 
    public int PlayerID { get; set; } 
    public int[] PlayerIds { set; get; } 
    public List<SelectListItem> Players { set; get; } 
} 

而且你以

@model WebGameProj.ViewModels.AddPlayersToGame 
<div> 
{ 
    @Html.ListBoxFor(x => x.PlayerIds, Model.Players) 

    <input type="submit" /> 
} 
</div> 

那麼你應該在模型中選擇IDS提交後。

0

你還可以嘗試:

型號

public class AddPlayersToGame 
{ 
    public string GameTitle { set; get; } 
    public int GameID { set; get; } 
    public int[] PlayerIDs { get; set; } 
    public MultiSelectList Players { get; set; } 
} 

控制器

public ActionResult AddPlayersView(int id) 
{ 
    var GameSelected = db.Games.Find(id); 

    if (GameSelected== null) 
    { 
     return HttpNotFound(); 
    } 

    var np = new AddPlayersToGame { GameID = id, GameTitle = GameSelected.GameTitle }; 

    var playerList = db.Players.Select(m => new 
    { 
     PlayerUserName = m.PlayerUserName, 
     PlayerId = m.PlayerId 
    }).ToList(); 

    np.Players = new MultiSelectList(playerList, "PlayerIDs", "PlayerUserName"); 
    return View(np); 
} 

[HttpPost] 
public ActionResult AddPlayersView(AddPlayersToGame model) 
{ 
    foreach (var playerID in model.PlayerIDs) 
    { 
     var SelPlayer = db.Players.Find(playerID); 

     if (SelPlayer== null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 

     if (SelPlayer != null) 
     { 
      Game GameSelected = new Game(); 
      GameSelected.GamePlayers.Add(SelPlayer); 

      db.Entry(GameSelected).State = EntityState.Modified; 
      db.SaveChanges(); 
     } 
    } 
    return RedirectToAction("GameDetailsView"); 
} 

查看

@model WebGameProj.ViewModels.AddPlayersToGame 

<div> 
{ 
    @Html.ListBoxFor(x => x.PlayerIDs, Model.Players) 
    <input type="submit" /> 
} 
</div> 
相關問題