asp.net mvc5 - 将选定的列表项ID传递回控制器

时间:2016-02-19 15:02:08

标签: c# asp.net-mvc asp.net-mvc-5

我有一个视图模型,其中包含我用于视图下拉列表的信息:

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方法将通过使用他们通过drop传回的ID来查找数据库中的每个玩家 - 视图中的下拉列表,然后将它们添加到该游戏的当前玩家列表中。

2 个答案:

答案 0 :(得分: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>

然后,您应该在提交后在模型中选择ID。

答案 1 :(得分:0)

您也可以尝试:

模型

SELECT @@TRANCOUNT 
BEGIN TRAN T1    
SELECT @@TRANCOUNT  
    BEGIN TRAN T2               
    SELECT @@TRANCOUNT  
    ROLLBACK TRAN T2        
SELECT @@TRANCOUNT 
ROLLBACK TRAN T1   
SELECT @@TRANCOUNT        

控制器

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