我有一个包含玩家下拉列表的创建页面
我在我的控制器中填充
[HttpGet]
public ActionResult Create()
{
var vm = new CreateMatchViewModel
{
Winner =
CreateWinnerList(),
PlayerList = CreatePlayerList()
}
;
return View(vm);
}
private IEnumerable<SelectListItem> CreatePlayerList()
{
List<Player> playerList = _playerManagementRepository.GetAllPlayers();
return playerList.Select(p => new SelectListItem
{
Text = p.Username,
Value = p.Id.ToString()
});
}
private SelectListItem[] CreateWinnerList()
{
return new[]
{
new SelectListItem {Text = "Player 1", Value = 1.ToString(), Selected = true}
, new SelectListItem {Text = "Player 2", Value = 2.ToString(), Selected = false}
};
}
这很好地填充了我的视图
@model TableTennis.ViewModels.CreateMatchViewModel
@{
ViewBag.Title = "Enter Match Result";
}
@using (Html.BeginForm("Create", "Match", FormMethod.Post))
{
<h4>Player 1</h4>
@Html.DropDownListFor(p => p.Player1ID, Model.PlayerList)
<h4>Player 2</h4>
@Html.DropDownListFor(p => p.Player2ID, Model.PlayerList)
<h4>Winner</h4>
@Html.DropDownListFor(w => w.WinnerID, Model.Winner)
<h5>Set 1</h5>
@Html.EditorFor(p => p.Score1Set1)
@Html.EditorFor(p => p.Score2Set1)
<h5>Set 2</h5>
@Html.EditorFor(p => p.Score1Set2)
@Html.EditorFor(p => p.Score2Set2)
<h5>Set 3</h5>
@Html.EditorFor(p => p.Score1Set3)
@Html.EditorFor(p => p.Score2Set3)
<input type="submit" value="Add result" />
}
然后在 Post 上,我在 ModelState 无效的地方进行了一些验证,因此运行以下命令
[HttpPost]
public ActionResult Create(CreateMatchViewModel vm)
{
try
{
if (!ModelState.IsValid)
{
vm.PlayerList = CreatePlayerList();
vm.Winner = CreateWinnerList();
return View(vm);
}
但这失败了以下错误,但我不确定如何将 Guid 转换为 SelectedListItem 以便在新的 Get 请求中再次选择列表中的相同项目
具有键“Player1ID”的 ViewData 项属于“System.Guid”类型,但必须属于“IEnumerable”类型。
视图模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace TableTennis.ViewModels
{
public class CreateMatchViewModel
{
public CreateMatchViewModel()
{
Score1Set1 = 0;
Score1Set2 = 0;
Score1Set3 = 0;
Score2Set1 = 0;
Score2Set2 = 0;
Score2Set3 = 0;
}
public IEnumerable<SelectListItem> PlayerList { get; set; }
public IEnumerable<SelectListItem> Winner { get; set; }
[Required]
public Guid Player1ID { get; set; }
[Required]
public Guid Player2ID { get; set; }
[Required]
public int WinnerID { get; set; }
[Required]
[RegularExpression("[0-9][0-9]?")]
public int Score1Set1 { get; set; }
[RegularExpression("[0-9][0-9]?")]
[Required]
public int Score1Set2 { get; set; }
[RegularExpression("[0-9][0-9]?")]
[Required]
public int Score1Set3 { get; set; }
[RegularExpression("[0-9][0-9]?")]
[Required]
public int Score2Set1 { get; set; }
[RegularExpression("[0-9][0-9]?")]
[Required]
public int Score2Set2 { get; set; }
[RegularExpression("[0-9][0-9]?")]
[Required]
public int Score2Set3 { get; set; }
}
}
编辑 这使它工作
if (!ModelState.IsValid)
{
ModelState.Clear();
vm.PlayerList = CreatePlayerList();
vm.Winner = CreateWinnerList();
return View(vm);
}
if (vm.Player1ID == vm.Player2ID)
{
ModelState.Clear();
vm.PlayerList = CreatePlayerList();
vm.Winner = CreateWinnerList();
return View(vm);
}