Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Modelbinding a List stopped working when moving from .net core 1.0 to .net core 1.1 .cshtml contains a form with a foreach loop

$
0
0

Hi all,

When upgrading to 1.1 my modelbinding from a form stopped working

public class test {

public int id {get;set;}

public string name {get;set;}

}

[HttpGet]

public async Task<IActionResult> doIt()

{

List<test> model = new List<test>();

test atest = new test();

atest.id = 1;

atest.name="First";

model.Add(atest);

atest = new test();

atest.id=2;

atest.name="Second";

model.Add(atest);

return View(model)

}

[HttpPost]

public async Task<IActionResult> doIt(List<test> model)

{

if(ModelState.IsValid)

{

//model.Count() is always Zero after upgrade to 1.1 :-)

}

}

//doIt.cshtml

@model List<test>

<form asp-action="doIt" method="post">

@foreach(test item in Model)

{

<label asp-for="@Model.First().id"></label>

<input type="hidden" asp-for="@item.id" value="@item.id" />

<label asp-for="@Model.First().name"></label>

<input type="hidden" asp-for="@item.name" value="@item.name" />
<button id="btnConfirm" type="submit" class="btn btn-info">Go On</button>

</form>
}

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>