Very basic Model:
public class Person
{
public string Name;
public int Age;
}
and very simple view:
@model DynWebPOC.Models.Person
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Hello, @Model.Name
<br/>
You're getting old at @Model.Age years old now!
@using(Html.BeginForm("Index","Test",FormMethod.Post))
{
<fieldset>
<label for="name" style="color: whitesmoke">Name:</label>
@Html.TextBoxFor(m => m.Name)
<br/>
<label for="age" style="color: whitesmoke">Age:</label>
@Html.TextBoxFor(m => m.Age)
<br/>
<input type="submit" value="Submit"/>
</fieldset>
}
And a very simple controller:
public class TestController : Controller
{
[HttpGet]
public ActionResult Index()
{
object model = new Person {Name = "foo", Age = 44};
return View(model);
}
[HttpPost]
public ActionResult Index(Person person)
{
return View();
}
}
When the screen loads, the values bind correctly to the page. But when I push submit button, the person object has all null values for age & name.
Because I used Html.TextBoxFor, shouldn't it have set up all the bindings correctly and the object should have automatically bound back to the POST? It binds just fine in the GET..
Did I miss something in the call to Html.BeginForm() maybe?
EditorForinstead ofTextBoxFor? - mattytommo