Submitting a POST using a ViewModel

I’m creating a website where you can Create an event for a game.I have a Create Page. The page has a GameID field when creating a new Event. I’m attempting to use a ViewModel so that I can display the name of the Game and select that, rather than the ID.

public class EventCreateVM
    {
        public Events Event { get; set; }
        public IEnumerable<SelectListItem> 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; }
}

I’ve altered the Create page so that it works with the ViewModel

@model AgileTeamFour.UI.ViewModels.EventCreateVM

@{
    ViewData["Title"] = "Create";
}
<h1>Create</h1>

<h4>Events</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" asp-controller="Event">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
           @*  <div class="form-group">
                <label asp-for="Event.EventID" class="control-label"></label>
                <input asp-for="Event.EventID" class="form-control" />
                <span asp-validation-for="Event.EventID" class="text-danger"></span>
            </div> *@
            <div class="form-group">
    <label asp-for="Event.GameID" class="control-label"></label>
    <select asp-for="Event.GameID" class="form-control" asp-items="Model.Games"></select>
    <span asp-validation-for="Event.GameID" class="text-danger"></span>
</div>
            <div class="form-group">
                <label asp-for="Event.EventName" class="control-label"></label>
                <input asp-for="Event.EventName" class="form-control" />
                <span asp-validation-for="Event.EventName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Event.Server" class="control-label"></label>
                <input asp-for="Event.Server" class="form-control" />
                <span asp-validation-for="Event.Server" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Event.MaxPlayers" class="control-label"></label>
                <input asp-for="Event.MaxPlayers" class="form-control" />
                <span asp-validation-for="Event.MaxPlayers" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Event.Type" class="control-label"></label>
                <input asp-for="Event.Type" class="form-control" />
                <span asp-validation-for="Event.Type" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Event.Platform" class="control-label"></label>
                <input asp-for="Event.Platform" class="form-control" />
                <span asp-validation-for="Event.Platform" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Event.Description" class="control-label"></label>
                <input asp-for="Event.Description" class="form-control" />
                <span asp-validation-for="Event.Description" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Event.DateTime" class="control-label"></label>
                <input asp-for="Event.DateTime" class="form-control" />
                <span asp-validation-for="Event.DateTime" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

And altered the controller.


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

The page makes the Game select list fine. But when I try pushing the submit button, the page just refreshes and it doesn’t create anything in the database.

The payload form seems fine, as far as I can tell. The post is 200 and doesn’t seem to encounter an error. The create function works fine if I’m not using the viewmodel changes. I’ve been trying for a few hours to figure it out but haven’t had any luck. Any advice would be greatly appreciated.

New contributor

Jimmy Dictus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Based on your code, the request is sent to the “Event/Create” API with GET instead of POST (HTTP method) when the form is submitted.

You can debug by placing the breakpoint in the first line of code for both “Event/Create” API (methods) with GET and POST. This will help you to determine whether the “Event/Create” API (with POST method) is hit.

In summary, add the method="POST" attribute to the <form> element to ensure that the form is submitted to the right API.

<form asp-action="Create" asp-controller="Event" method="POST">
    ...
</form>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật