0

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[0].Status:0
row[1].date:02/05/2017 00:00:00
row[1].Status:0
row[2].date:03/05/2017 00:00:00
row[2].Status:0
row[3].date:04/05/2017 00:00:00
row[3].Status:0
row[4].date:05/05/2017 00:00:00
row[4].Status:0
...

My model

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

Basically is is my code:

//HomeController : Controller

[HttpGet("/status")]
public IActionResult Status()
{
    return View();
}

[HttpPost("/status")]
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">
        <thead>
            <tr>
                <th>Date</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            @{
                DateTime startDate = new DateTime(2017, 05, 01);
            }
            @for (int i = 0; i < 20; i++)
            {
                var date = startDate.AddDays(i);
                <tr>

                    <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())" />
                    </td>
                    <td>
                        <select name="row[@i].Status">
                            <option value="0" selected>Working</option>
                            <option value="1">Bug</option>
                        </select>
                    </td>
                </tr>
            }
        </tbody>
    </table>

</form>

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

Community
  • 1
  • 1
Lenny32
  • 624
  • 6
  • 15

1 Answers1

2

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) :)

univ
  • 717
  • 4
  • 12