Note that there are some explanatory texts on larger screens.

plurals
  1. POMy classes are empty/null in the viewModel, when creating an object. MVC, EF
    primarykey
    data
    text
    <p>I got my createPlayer view. It works and loads the images I got stored. And I want to choose one image in the dropdown and store that ID on my playerImageId. But both my classes (images / player) are empty in the <code>[HttpPost]</code> controller, when hitting the create button.</p> <p>The model that is send to the view is this:</p> <pre><code>public ActionResult CreatePlayer() { var images = db.Image.ToList(); var playerViewModel = new PlayerViewModel(new Player(), images); return View(playerViewModel); } </code></pre> <p>The view looks like this:</p> <pre><code>@model MariedalsIK.Models.PlayerViewModel @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Player&lt;/legend&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.Image) &lt;/div&gt; &lt;div&gt; @Html.DropDownListFor(model =&gt; model.Image, Model.Image.Select(x =&gt; new SelectListItem { Text = x.ImageName, Value = x.Id.ToString() })) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Player.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Player.Name) @Html.ValidationMessageFor(model =&gt; model.Player.Name) &lt;/div&gt; </code></pre> <p>The PlayerViewModel looks like this:</p> <pre><code> public class PlayerViewModel { public PlayerViewModel(Player player, IEnumerable&lt;Image&gt; images) { Player = player; Image = images; } public Player Player { get; private set; } public IEnumerable&lt;Image&gt; Image { get; private set; } public PlayerViewModel() { } } </code></pre> <p>And the CreatePlayer action looks like this:</p> <pre><code>[HttpPost] public ActionResult CreatePlayer(PlayerViewModel playerViewModel) { if (ModelState.IsValid) { db.Player.Add(playerViewModel.Player); db.SaveChanges(); return RedirectToAction("Teams"); } return RedirectToAction("Teams"); } </code></pre> <p>The thing is that it works if I only sends a player to <code>[HttpPost]</code> CreatePlayer, like this:</p> <pre><code>[HttpPost] public ActionResult CreatePlayer(Player player) </code></pre> <p>But in that case I don't get the <code>imageId</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload