Lenny32 Lenny32 - 1 year ago 187
HTML Question

ASP.NET CORE mapping complex POST [FromForm] pure html/http protocol

I am working on a View that sends an "Array" of data to the server through the method POST from a browser (Chrome). No Json/Javascript involved

Here is what the browser is sending:

row[0].date:01/05/2017 00:00:00
row[1].date:02/05/2017 00:00:00
row[2].date:03/05/2017 00:00:00
row[3].date:04/05/2017 00:00:00
row[4].date:05/05/2017 00:00:00

My model

public class Info
public int Status { get; set; }
public string Date { get; set; }

Basically is is my code:

//HomeController : Controller

public IActionResult Status()
return View();

public void StatusPost(Info[] rows)
var data = System.Text.Encoding.UTF8.GetBytes($"rows = {rows.Length}");
this.Response.Body.Write(data, 0, data.Length);

In the StatusPost method, I tried to add the attribute [FromBody], [FromForm].
I also tried with A list of object instead too.

I also tried with a master object like that:

public class row
public List<info> info { get; set; }
public class info
public int status { get; set; }
public string date { get; set; }

The Html/Razor

<form method="post" action="/status" enctype="application/x-www-form-urlencoded">
<button type="submit">Submit</button>
<table class="table table-bordered">
DateTime startDate = new DateTime(2017, 05, 01);
@for (int i = 0; i < 20; i++)
var date = startDate.AddDays(i);

<td style=" text-align: center; vertical-align: middle;" rowspan="1">
@(date.ToString("ddd, MMM yyyy"))
<input type="hidden" name="row[@i].date" value="@(date.ToString())" />
<select name="row[@i].Status">
<option value="0" selected>Working</option>
<option value="1">Bug</option>


I tried with "multipart/form-data" and "application/x-www-form-urlencoded" for the enctype of the form.

I tried so much that now I am lost and I do not know how to make the binding of the server side without having to create my object manually by parsing the Request.Post dictionary.

Is it possible?
How is this way of sending data to the server is called?

I saw this post and I tried but did not work either:
Asp.Net Core Post FromBody Always Null

Answer Source

Lol you are definitely not going to like the answer for this one. In your view you need to change your field name (row) to rows so that it matches the name of the controller action parameter.

For correctness purposes you should use Date instead of date in your view's hidden field (but it will work even if you don't change it) :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download