Код: Выделить всё
public class EventCreateVM
{
public Events Event { get; set; }
public IEnumerable Games { get; set; }
}
Код: Выделить всё
public class Events
{
public int EventID { get; set; }
public int GameID { get; set; }
[DisplayName("Event Name")]
public string EventName { get; set; }
public string Server { get; set; }
[DisplayName("Max Players")]
public int MaxPlayers { get; set; }
public string Type { get; set; }
public string Platform { get; set; }
public string Description { get; set; }
[DisplayName("Event Time")]
public DateTime DateTime { get; set; }
}
Код: Выделить всё
@model AgileTeamFour.UI.ViewModels.EventCreateVM
@{
ViewData["Title"] = "Create";
}
Create
[h4]Events[/h4]
@*
*@
Back to List
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Код: Выделить всё
public ActionResult Create()
{
var games = GameManager.Load();
var viewModel = new EventCreateVM
{
Event = new Events(),
Games = games.Select(g => new SelectListItem
{
Value = g.GameID.ToString(),
Text = g.GameName
}).ToList()
};
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(EventCreateVM viewmodel)
{
try
{
if (ModelState.IsValid)
{
int eventID = 0;
EventManager.Insert(ref eventID, viewmodel.Event.GameID, viewmodel.Event.EventName, viewmodel.Event.Server, viewmodel.Event.MaxPlayers, viewmodel.Event.Type, viewmodel.Event.Platform, viewmodel.Event.Description, viewmodel.Event.DateTime);
return RedirectToAction(nameof(Index));
}
return View(viewmodel);
}
catch
{
return View();
}
}
Насколько я могу судить, форма полезной нагрузки выглядит нормально. Сообщение имеет номер 200 и, похоже, не содержит ошибок. Функция создания работает нормально, если я не использую изменения модели представления. Я пытался понять это несколько часов, но безуспешно. Будем очень признательны за любые советы.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -viewmodel
Мобильная версия