Web API model binding problem in POST

I'm new to the Web API, and it really puzzles me.

Here is the model:

public class Model { public string firstname { get; set; } public string lastname { get; set; } } 

here is the controller:

 public class TestController : ApiController { [HttpPost] public void Test(Model request) { } } 

I use the html form for publishing and it will get to the controller:

 <html> <head> <title></title> </head> <body> <form action="/Test" method="post"> <input type="text" name="​firstname" value="test1" /> <input type="text" name="lastname" value="test2" /> <input type="submit" value="Submit" /> </form> </body> </html> 

However, when I debug the controller, the model object has firstname=null and lastname correctly.

What am I doing wrong?

+4
source share
1 answer

So this is the weirdest thing I've ever seen. Use this as your opinion:

 <html> <head> <title></title> </head> <body> <form action="/Home" method="post"> @*<input type="text" name="​firstname" value="test1" />*@ <input type="text" name="firstname" value="test1" /> <input type="text" name="lastname" value="test2" /> <input type="submit" value="Submit" /> </form> </body> </html> 

EDIT: Not sure if you did this, but basically your code is name="&#8203;firstname" . Not sure if you wanted to add this there to communicate with people, but you have a special character.

0
source

Source: https://habr.com/ru/post/1434081/


All Articles