I'm trying to get a post request to work with the web api. Following is my api controller.
public class WebsController : ApiController
{
[HttpPost]
public void PostOne(string id)
{
}
[HttpPost]
public void PostTwo(Temp id)
{
}
}
I have altered the webapi route to take the action into account. the Temp model look something like this.
public class Temp
{
public string Id { get; set; }
}
my view look something like this
@using (Ajax.BeginForm(new AjaxOptions
{
Url = "/api/webs/postone",
HttpMethod = "post"
}))
{
<input name="id" id="id" value="2" />
<input type="submit" value="submit" />
}
the above code does not work at all with the postone unless I put the [FromBody] attribute in front of the parameter like this.
[HttpPost]
public void PostOne([FromBody]string id)
{
}
then it hits the action, but the id is still null. It doesn't get populated with the value in the textbox.
But, if I change the Url
of the Ajax.BeginForm
to posttwo
which take the model Temp
, it works nicely and the Id
field gets the proper value in the textbox.
can anyone please explain me the reason for this to happen and how I can post a simple value to a web api action? I mean, why can it bind a complex type but not a simple type.