In Index() I return the View with the Model AnlassDetailModel in there i have the Object Anlass and List<SelectListItem> and both of them are filled with data.
I use this list for a dropdown in a form in the View.
However, when I want to submit the form, the Object Anlass is Null, but the Variable from the Dropdown has the Value.
How can I get the Model with data in the objects?
Model:
public class AnlassDetailModel
{
public Anlass Anlass { get; set; }
public List<SelectListItem> Kategories = new List<SelectListItem>();
public int KategorieId { get; set; }
}
Controller:
public ActionResult Index()
{
AnlassDetailModel model = new AnlassDetailModel()
{
Anlass = Anlass.GetAnlass(),
Kategories = Kategorie.GetKategorieBezeichnung()
};
return View("~/Views/AnlassDetail.cshtml", model);
}
[HttpPost]
public ActionResult Cat(AnlassDetailModel model)
{
//Here the Model only has value in KategorieId but not in Anlass
if (model.Anlass.Kategories == null)
model.Anlass.Kategories = new List<Kategorie>();
model.Anlass.Kategories.Add(Kategorie.GetKategorieById(model.KategorieId));
return View("~/Views/AnlassDetail.cshtml", model);
}
View:
<div>
@using (Html.BeginForm("Cat", "Anlass", FormMethod.Post))
{
@Html.DropDownListFor(m => m.KategorieId, new SelectList(Model.Kategories, "Value", "Text"), "Auswählen")
<input type="submit" value="Hinzufügen" />
}
</div>