4

I am new to Web API and this is really puzzling to 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 am using an html form to post and it will hit 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 into the controller, the model object has firstname=null and lastname comes up correctly.

What I am doing wrong?

GargantuChet
  • 5,691
  • 1
  • 30
  • 41
user1669811
  • 835
  • 3
  • 12
  • 15
  • If this is a direct copy and paste try replacing `" firstname"` with `"firstname"` – Matthew Whited Oct 18 '12 at 15:40
  • 1
    I would try 2 things: add [FromBody] before Model in Test method declaration and check ModelState errors in Test method (http://stackoverflow.com/questions/1352948/how-to-get-all-errors-from-asp-net-mvc-modelstate) – kyrisu Nov 22 '12 at 11:41
  • Assuming the above doesn't work, I would also try firing off a direct jQuery `.ajax()` request in Chrome, and seeing what happens in the console. Something like: `$.ajax('/Test', { type: 'POST', data: { firstname: 'test1', lastname: 'test2' }, success: function (data) { console.log(data); } })` – Troy Alford Feb 28 '13 at 20:13
  • You can use Fiddler2 Post composer to test your WEB Api functionality. If that is working fine, look into your web page. – T.S. Aug 19 '13 at 21:57

1 Answers1

0

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

<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 this name="&#8203;firstname". Not sure if you meant to add that in there to mess with people but you have a special character there