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 7532295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:24:37+00:00 2026-05-30T05:24:37+00:00

I created this viewmodel: public class PlayerViewModel { PlayerRepository repo = new PlayerRepository(); public

  • 0

I created this viewmodel:

public class PlayerViewModel
{
    PlayerRepository repo = new PlayerRepository();

    public Player Player { get; set; }
    public int SelectedUserID { get; set; }
    public SelectList Users { get; set; }


    public PlayerViewModel()
    {
        Player = new Player();
    }
    public PlayerViewModel(int id)
    {
        Player = repo.Retrieve(id);
        Users = new SelectList(repo.GetUsers());
        SelectedUserID = 0;
    }
}

this I have in view:

@Html.DropDownListFor(x => x.SelectedUserID, Model.Users)
        @Html.ValidationMessageFor(x => x.SelectedUserID)

and this in controller:

[Authorize]
        public ActionResult Upravit(int id)
        {
            var playerview = new PlayerViewModel(id);
            return View(playerview);
        }
        [Authorize,HttpPost]
        public ActionResult Upravit(int id, PlayerViewModel playerView)
        {
            if (ModelState.IsValid)
            {
                playerView.Player.User = usRepo.GetUserById(playerView.SelectedUserID);
                repo.Save(playerView.Player);
                return RedirectToAction("Podrobnosti", new { id = playerView.Player.PlayerID });
            }
            return View(playerView);
        }

Now I have problem that ” The field SelectedUserID must be a number.” and I have in dropdownlist UserName. I modified this many times, I tried with Dictionary and other ways but everyway has some problem. So I want just ask for best way to add custom class User to class Player.
Player class:

public class Player
    {
        // pokud použijeme virtual a vlastností tak nám EF rozšíří o další možnosti jako lazy loading a další
        [Key]
        public int PlayerID { get; set; }

        [Required]
        public string Name { get; set; }
        [Required]
        public string Surname { get; set; }

        public string PhotoUrl { get; set; }

        public string Post { get; set; }

        public virtual Team Team { get; set; }

        public virtual User User { get; set; }
        // public int UserID { get; set; }

        //public virtual ICollection<Article> Articles { get; set; }
        // Here could be next things as number, ...

    }

Thanks

  • 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-05-30T05:24:39+00:00Added an answer on May 30, 2026 at 5:24 am

    Use this constructor instead:
    http://msdn.microsoft.com/en-us/library/dd505286.aspx

    public SelectList(
        IEnumerable items,
        string dataValueField,
        string dataTextField
    )
    

    Something like this:

    Users = new SelectList(repo.GetUsers(),"UserID", "UserName");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given domain model... public class Entity { public int Id { get; set; }
I have a main page which uses a ViewModel I have created: public class
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
I have created this code, and when I run it, don't get any errors
I have a class user which looks like this: public class User { public
I have created a viewmodel class for my custom view template. Right now I
I've created the following view model: public class PropertyViewModel { public PropertyViewModel(Property property, IList<PropertyImage>
I created this layout of successive text input fields, 1- Enter data into empty
I created this regular expression to validate names: ^[a-zA-Z0-9\s\-\,]+.\*?$ Is there a way add
I created this regex to match all words that start with @ in my

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.