Dreadhorn
Dreadhorn

Reputation: 43

Objects in Model are Null after Post

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>

Upvotes: 2

Views: 82

Answers (2)

Mohamed Salah
Mohamed Salah

Reputation: 975

The issue is the data model.

When you passed the model in the first request you filled the model with the value

  public ActionResult Index()
  {
         AnlassDetailModel model = new AnlassDetailModel()
        {
               Anlass = Anlass.GetAnlass(),
                Kategories = Kategorie.GetKategorieBezeichnung()
         };
        return View("~/Views/AnlassDetail.cshtml", model);
   }

Then these data filled in the view the controls in the view then when you try to post the data this is a new request the only data passed is the data binded to the controls in the view.

So if you want to refill the data after posting the data so you will need to refill the data again for the controls.

  [HttpPost]
  public ActionResult Cat(AnlassDetailModel model)
  {
        AnlassDetailModel model = new AnlassDetailModel()
        {
                Anlass = Anlass.GetAnlass(),
                Kategories = Kategorie.GetKategorieBezeichnung()
          };
         return View("~/Views/AnlassDetail.cshtml", model);
    }

Upvotes: 1

Volfra
Volfra

Reputation: 114

This might work. Replace it. You must save it to send it to database.

      [HttpPost]
    public ActionResult Cat(AnlassDetailModel model)
    {
        if (model.Anlass.Kategories == null)
            model.Anlass.Kategories = new List<Kategorie>();
        model.Anlass.Kategories.Add(Kategorie.GetKategorieById(model.KategorieId));
        model.SaveChanges();
        return View("~/Views/AnlassDetail.cshtml", model);
    }

Upvotes: 0

Related Questions