VSO
VSO

Reputation: 12656

Cannot POST Data in .NET Core MVC

I am trying to make a POST request using .NET CORE MVC by submitting a form. The call is successfully made from the view and looks like this:

enter image description here

My controller code looks like this:

[HttpPost("fred-responses-report-result/{formId:int?}")]
public async Task<IActionResult> FredResponsesReportResult(int? formId, ReportFilters filters)

The formId param is passed through fine and set to 63, but the report filters object (below) is just null. The class is below, if it matters:

public class ReportFilters
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime FromDate { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ToDate { get; set; }
}

So far I have read this article and this post. I have tried removing the attributes from the model - didn't help. I tried pre-pending [FromBody] and [FromForm] to the controller param, and it resulted in a 415 http error.

I am at a bit of a loss at how to get this to work at this point. If I look at the "view source" (in the screenshot) it seems like these are just being passed as HTML-encoded strings, rather than JSON, though I don't understand why. Regardless, what is the proper approach here?

EDIT, here is the code making the call:

            <form action="" method="post">
            <div class="row gray-background">
                <!--Date Filter-->
                <div class="col-12">
                    <div class="row">
                        <div class="col-2 offset-1 bold-font">
                            <p>Submitted Date</p>
                        </div>
                        <div class="col-3">

                            <div class="date datepicker fromDate">
                                <label for="fromDate">From</label>
                                <div class="input-group">
                                    <input id="fromDate" name="fromDate" type="text" class="form-control" value="1">
                                    <span class="input-group-addon">
                                        <i class="fa fa-calendar fa-lg"></i>
                                    </span>
                                </div>
                            </div>

                        </div>
                        <div class="col-3">
                            <div class="date datepicker toDate">
                                <label for="toDate">To</label>
                                <div class="input-group">
                                    <input id="toDate" name="toDate" type="text" class="form-control" value="@Model.ReportFilters.ToDate">
                                    <span class="input-group-addon">
                                        <i class="fa fa-calendar fa-lg"></i>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!--Update Filters Button-->
                    <div class="row">
                        <div class="col-12 d-flex justify-content-center">
                            <button class="btn button-margin btn-primary"
                                    type="submit">
                                Update Filter
                            </button>
                        </div>
                    </div>

                </div>
            </div>
        </form>

Upvotes: 2

Views: 4096

Answers (2)

mvermef
mvermef

Reputation: 3924

[HttpPost("fred-responses-report-result/{formId:int?}")]
public async Task<IActionResult> FredResponsesReportResult(int? formId,[Bind("ToDate","FromDate")]ReportFilters filters)
 {

this should work. Since you are using straight models (i believe) this instance, bind will allow you to target only the fields you wanted to be returned from the form..

without seeing the rest of the code associated with the form on the actual view and if you aren't doing AJAX posts (content-type not set for application/json) then [FromBody] will never work.

The next question is the route used actually expecting a variable named formId or is it using the default id?

Upvotes: 2

tearius
tearius

Reputation: 58

Try to submit form data in this format:

filters.fromDate: ..
filters.toDate: ...

instead of

fromDate: ...
toDate: ...

Upvotes: 0

Related Questions