I'm creating a simple little website that interfaces with a web API, that allows users to have a database of emus. I am currently working on the PUT action in the controller. The put first goes from an action that just redirects to a new View. This View then prompts the user for information, and returns that in a model. A second action should then handle the information and call the API.
The first and second Action have the same name, so the second action that handles the API request has the [HttpPut] tag on top. However, if it has that tag, then the View never goes to the second Action-it just reloads the page when I press submit. However, if I put [HttpPost], it works just fine.
I have already tried changing the configuration in IIS Express, to no avail.
Here is the code for the View:
@model HelloEmuWebsite.Models.EmuItem
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.SearchName)
@Html.TextBoxFor(x => x.SearchName)
</div>
<div>
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
</div>
<div>
@Html.LabelFor(x => x.Age)
@Html.TextBoxFor(x => x.Age)
</div>
<div>
@Html.LabelFor(x => x.Weight)
@Html.TextBoxFor(x => x.Weight)
</div>
<input type="submit" value="OK" />
}
And here is the code in the controller:
public IActionResult ChangeOneEmu()
{
return View(new EmuItem());
}
[HttpPut]
async public Task<IActionResult> ChangeOneEmu(EmuItem model)
{
var baseAddr = new Uri("my_url/api/values/");
var client = new HttpClient { BaseAddress = baseAddr };
var response = await client.PutAsJsonAsync(model.SearchName, model);
return RedirectToAction("Index");
}
And here is the code for the PUT request in my custom API:
[HttpPut("{id}")]
public ActionResult<List<Models.EmuItem>> Put(string id, [FromBody] Models.EmuItem ChangedEmu)
{
if (ModelState.IsValid)
{
return MyEmus.ChangeEmu(id, ChangedEmu);
}
return BadRequest(ModelState);
}
Why does this happen? How can I fix it so that it will accept [HttpPut]? Alternatively, is there a better way to do this that does not rely on the [http] tags? Thanks!