With reference to ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response,
I've been attempting to make API changes (Changing the Data annotations, changing the path, explicitly declaring the params with data annotations and even making sure asp-action does the right thing on the view.)
But to no avail, here's the stack trace.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action CounterCore.Controllers.AdvertController.Modify (CounterCore) in 70.696ms
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action CounterCore.Controllers.AdvertController.Modify (CounterCore) in 70.696ms
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action CounterCore.Controllers.AdvertController.Modify (CounterCore) in 70.696ms
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action CounterCore.Controllers.AdvertController.Modify (CounterCore) in 70.696ms
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 1536.417ms 200
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 1536.417ms 200
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 1536.417ms 200
Notice something?
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Doesn't give me any useful information to tap on...
The view is generated from this method:
[Authorize]
[HttpGet("{id}")]
public async Task<IActionResult> Modify(long id)
That returns the ViewModel,
Which will POST to this method via the standard Razor view:
[Authorize]
[HttpPost("{id}")]
public async Task<IActionResult> Modify(long id,[FromForm]ModifyViewModel model)
I did remove the FromForm and all that kinda stuff, nothing works. It returns me a useless one liner Exception..
View:
<form class="form side-gap rounded" id="form" asp-route-returnUrl="@ViewData["ReturnUrl"]" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<h3>Edit your advert</h3>
<hr class="my-5" />
<div class="form-group row">
<h5 for="typeDropdown" class="col-sm-2 col-form-label">I want to</h5>
<div class="col-sm-10 input-group">
<div id="typeDropdown" class="dropdown">
<button type="button" id="typeDropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-primary dropdown-toggle">
@Model.AdvertType.Name
</button>
<div aria-labelledby="dropdownMenuButton" id="typeDropdownMenu" class="dropdown-menu type">
@{
foreach (var advertType in Model.AdvertTypes)
{
Output.Write("<a class=\"dropdown-item type\" value=\"{1}\">{0}</a>", advertType.Name, advertType.Id);
}
}
</div>
</div>
<input asp-for="AdvertTypeId" id="typeDropdownInput" type="hidden" />
</div>
</div>
<div class="form-group row">
<h5 for="walletTypeDropdown" class="col-sm-2 col-form-label">Cryptocurrency</h5>
<div class="col-sm-10 input-group">
<div id="walletTypeDropdown" class="dropdown">
<button type="button" id="walletTypeDropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-primary dropdown-toggle">
@Model.WalletType.Name
</button>
<div aria-labelledby="walletTypeButton" id="walletTypeDropdownMenu" class="dropdown-menu walletType">
@{
foreach (var walletType in Model.WalletTypes)
{
Output.Write("<a class=\"dropdown-item type\" value=\"{1}\">{0} ({2})</a>", walletType.Name, walletType.Id, walletType.CurrencyName);
}
}
</div>
</div>
<input asp-for="WalletTypeId" id="walletTypeDropdownInput" type="hidden" />
</div>
</div>
<hr class="my-3" />
<div class="form-group row">
<h5 class="col-sm-2 col-form-label">Location</h5>
<div class="col-sm-4">
<input id="locationInput" type="text" class="form-control" />
<input asp-for="MeetingPlace" id="locationInputValue" type="hidden" />
</div>
</div>
<hr class="my-3" />
<div class="form-group row">
<h5 for="paymentTypeDropdown" class="col-sm-2 col-form-label">Payment Method</h5>
<div class="col-sm-10 input-group">
<div id="paymentTypeDropdown" class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" id="paymentTypeButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
@Model.PaymentType.Name
</button>
<div class="dropdown-menu paymentType" aria-labelledby="paymentTypeButton">
@{
foreach (var paymentType in Model.PaymentTypes)
{
Output.Write("<a class=\"dropdown-item paymentType\" value=\"{1}\">{0}</a>", paymentType.Name, paymentType.Id);
}
}
</div>
</div>
<input asp-for="PaymentTypeId" id="paymentTypeDropdownInput" type="hidden" />
</div>
</div>
<div class="form-group row">
<h5 class="col-sm-2 col-form-label">Currency</h5>
<div class="col-sm-10 input-group">
<div id="fiatCurrencyDropdown" class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" id="fiatCurrencyButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
@Model.FiatCurrency.Name (@Model.FiatCurrency.PairChar)
</button>
<div class="dropdown-menu fiatCurrency" aria-labelledby="fiatCurrencyButton">
@{
if (Model.FiatCurrencies != null)
{
foreach (var fiatCurrency in Model.FiatCurrencies)
{
Output.Write("<a class=\"dropdown-item fiatCurrency\" value=\"{1}\">{0} ({2})</a>", fiatCurrency.Name, fiatCurrency.Id, fiatCurrency.PairChar);
}
}
}
</div>
</div>
<input asp-for="FiatCurrencyId" id="fiatCurrencyDropdownInput" type="hidden" />
</div>
</div>
<hr class="my-3" />
<div class="form-group row">
<h5 class="col-sm-2 col-form-label">Margin</h5>
<div class="col-sm-2">
<div class="input-group">
<input asp-for="Margin" type="number" class="form-control" placeholder="0">
<div class="input-group-append">
<span class="input-group-text" id="MarginPctAppend">%</span>
</div>
</div>
</div>
<!--<div class="col-sm-3">
<div class="form-check form-check-inline checkbox">
<label asp-for="EquationMode" class="form-check-label">
<input asp-for="EquationMode" class="form-check-input" type="checkbox" id="eqnModeCheckBox"> Equation Mode (This will replace margin with an equation entry)
</label>
</div>
</div>-->
</div>
<div class="form-group row">
<h5 class="col-sm-2 col-form-label">Restrict amounts to</h5>
<div class="col-sm-2 form-inline form-group">
<input asp-for="AmountRestriction" type="number" class="form-control" placeholder="0">
<small id="passwordHelpInline" class="text-muted">
Optional. Restrict trading amounts to specific denominations.
</small>
</div>
</div>
<div class="form-group row">
<h5 class="col-sm-2 col-form-label">Transaction Limits</h5>
<div class="input-group col-sm-3">
<div class="input-group-prepend">
<span class="input-group-text" id="MinTxnLimitPrepend">Minimum</span>
</div>
<input asp-for="MinTxnLimit" type="number" class="form-control" placeholder="0" aria-describedby="MinTxnLimitPrepend">
</div>
<div class="input-group col-sm-3">
<div class="input-group-prepend">
<span class="input-group-text" id="MaxTxnLimitPrepend">Maximum</span>
</div>
<input asp-for="MaxTxnLimit" type="number" class="form-control" placeholder="1000" aria-describedby="MaxTxnLimitPrepend">
</div>
</div>
<hr class="my-3" />
<div class="form-group">
<div class="form-check">
<label asp-for="AllowOnlyIdenfitiedUser" class="form-check-label">
<input asp-for="AllowOnlyIdenfitiedUser" class="form-check-input" type="checkbox">Require Identity Verification
</label>
</div>
<div class="form-check">
<label asp-for="AllowOnlyPhoneVerifiedUser" class="form-check-label">
<input asp-for="AllowOnlyPhoneVerifiedUser" class="form-check-input" type="checkbox">SMS Verification Required
</label>
</div>
<div class="form-check">
<label asp-for="AllowOnlyTrustedUser" class="form-check-label">
<input asp-for="AllowOnlyTrustedUser" class="form-check-input" type="checkbox">Allow only trusted people
</label>
</div>
<div class="form-check">
<label asp-for="AllowOnlyTradeAfterIdentifyingUser" class="form-check-label">
<input asp-for="AllowOnlyTradeAfterIdentifyingUser" class="form-check-input" type="checkbox">Allow trades only after going through your validation process
</label>
</div>
<div class="form-check">
<label asp-for="AllowOnlyWithRealName" class="form-check-label">
<input asp-for="AllowOnlyWithRealName" class="form-check-input" type="checkbox">Allow only people with their real names
</label>
</div>
</div>
<hr class="my-3" />
<div class="form-group row">
<h5 class="col-sm-2 col-form-label">Terms of Advert</h5>
<div class="col-sm-7">
<textarea asp-for="TermsAndCondition" class="form-control" rows="7">@Model.TermsAndCondition</textarea>
</div>
</div>
<input asp-for="Id" type="hidden">
<!-- https://stackoverflow.com/questions/8054165/using-put-method-in-html-form -->
<input type="hidden" name="_METHOD" value="PUT"/>
<button type="submit" class="btn btn-primary">Update advert</button>
</form>