i can't seem to find the answer online...
I have a form where people can add persons. However the person I receive from the post request is empty.
My personModel has a few properties , Naam, Leeftijd and Hobbie.
My Create view has a form made with @Html.LabelFor.
Model:
public class PersoonModel
{
public string Naam { get; private set; }
public int Leeftijd { get; private set; }
public string Hobbie { get; private set; }
public PersoonModel(string naam,int leeftijd, string hobbie )
{
Naam = naam;
Leeftijd = leeftijd;
Hobbie = hobbie;
}
public PersoonModel()
{
}
}
View:
@using (Html.BeginForm("Create","Dashboard",FormMethod.Post))
{
<fieldset>
<legend>Persoon</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Naam)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Naam)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Leeftijd)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Leeftijd)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Hobbie)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Hobbie)
</div>
<input type="submit" value="Create new Person"/>
</fieldset>
Controller:
[HttpGet]
public ActionResult Create()
{
PersoonModel persoonModel = new PersoonModel();
return View(persoonModel);
}
[HttpPost]
public ActionResult Create(PersoonModel Persoon)
{
personen.Add(Persoon);
return Redirect("/Dashboard");
}
I can't seem to get the layout good on stackoverflow, but I hope you understand it The persoonmodel Persoon(in my Controller) is empty
<form>tag (orBeginFormcommand, whichever you use)?