Skip to content

Instantly share code, notes, and snippets.

@leppie
Last active January 29, 2020 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leppie/c22229b517308fa771420a3feb47ad89 to your computer and use it in GitHub Desktop.
Save leppie/c22229b517308fa771420a3feb47ad89 to your computer and use it in GitHub Desktop.
Wat am I doing wrong????
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Wat.Models;
namespace Wat.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public IActionResult Index()
{
var model = new HomeViewModel
{
Items = new List<HomeItem>
{
new HomeItem { Name = "A", Value = "A" },
new HomeItem { Name = "B", Value = "B" },
new HomeItem { Name = "C", Value = "C" },
}
};
return View(model);
}
[HttpPost]
public IActionResult Index(HomeViewModel model)
{
model.Items.RemoveAt(1);
// apparently this is the solution, very broken IMO
ModelState.Clear();
return View(model); // I expect to see (A,A),(C,C) but I get (A,A),(B,B)
}
}
}
namespace Wat.Models
{
public class HomeViewModel
{
public List<HomeItem> Items { get; set; }
}
public class HomeItem
{
public string Name { get; set; }
public string Value { get; set; }
}
}
@model HomeViewModel
<form method="post">
<table>
@for (int i = 0; i < Model.Items.Count; i++)
{
<tr>
<td><input asp-for="@Model.Items[i].Name" /></td>
<td><input asp-for="@Model.Items[i].Value" /></td>
</tr>
}
</table>
<input type="submit" value="Submit">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment