Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:26:17+00:00 2026-06-02T08:26:17+00:00

I got my createPlayer view. It works and loads the images I got stored.

  • 0

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 [HttpPost] controller, when hitting the create button.

The model that is send to the view is this:

public ActionResult CreatePlayer()
{
    var images = db.Image.ToList();
    var playerViewModel = new PlayerViewModel(new Player(), images);
    return View(playerViewModel);
}

The view looks like this:

@model MariedalsIK.Models.PlayerViewModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Player</legend>
        <div>
            @Html.LabelFor(model => model.Image)
        </div>
        <div>
            @Html.DropDownListFor(model => model.Image, Model.Image.Select(x => new SelectListItem
                                                                              {
                                                                                  Text = x.ImageName,
                                                                                  Value = x.Id.ToString()
                                                                              }))
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Player.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Player.Name)
            @Html.ValidationMessageFor(model => model.Player.Name)
        </div>

The PlayerViewModel looks like this:

  public class PlayerViewModel
    {
        public PlayerViewModel(Player player, IEnumerable<Image> images)
        {
            Player = player;
            Image = images;
        }

        public Player Player { get; private set; }
        public IEnumerable<Image> Image { get; private set; }

        public PlayerViewModel()
        {

        }
    }

And the CreatePlayer action looks like this:

[HttpPost]
public ActionResult CreatePlayer(PlayerViewModel playerViewModel)
{
    if (ModelState.IsValid)
    {
        db.Player.Add(playerViewModel.Player);
        db.SaveChanges();
        return RedirectToAction("Teams");
    }
    return RedirectToAction("Teams");
}

The thing is that it works if I only sends a player to [HttpPost] CreatePlayer, like this:

[HttpPost]
public ActionResult CreatePlayer(Player player)

But in that case I don’t get the imageId.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:26:20+00:00Added an answer on June 2, 2026 at 8:26 am

    Your view model properties only have private setters. The default model binder cannot set their values. So change your view model like this:

    public class PlayerViewModel
    {
        public Player Player { get; set; }
        public IEnumerable<Image> Image { get; set; }
    }
    

    Also your dropdownlist is incorrect. You are attempting to bind it to a complex collection property. You need a simple scalar property to bind the selected value to:

    public class PlayerViewModel
    {
        public Player Player { get; set; }
    
        public IEnumerable<Image> Images { get; set; }
        public string ImageId { get; set; }
    }
    

    and in the view:

    <div>
        @Html.LabelFor(x => x.ImageId)
    </div>
    <div>
        @Html.DropDownListFor(
            x => x.ImageId, 
            new SelectList(Model.Images, "Id", "ImageName")
        )
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got an UIIMagePickerController in my newPlayerVC that saves an thumbnail image (after I resized
Got a programm with 2 threads. one thread is writing some stuff into the
Got this error message while trying to load view: The model item passed into
Got a seg fault from my memcpy that gdb can't give me anything else
got some problems. Built an applet that has to be used step-by-step. After each
Got datatable with Id, parentId, description. It is a relationial table structure. I want
I am building a AS3 only project and got runtime error that said Cannot
Got a issue with Facebook Graph API. I want to read page notifications via
Got that error when trying to push updated app to Heroku (probably the same
got a question, is it possible to hide image and show iframe which are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.