Last active
January 29, 2020 12:00
-
-
Save leppie/c22229b517308fa771420a3feb47ad89 to your computer and use it in GitHub Desktop.
Wat am I doing wrong????
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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