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

The Archive Base Latest Questions

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

This is my ViewModel: public class PlayerViewModel { PlayerRepository repo = new PlayerRepository(); public

  • 0

This is my 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(Player player)
        {
            Player = player;

            SelectedUserID = 0;
        }
   }

This is in my controller (it´s for editing):

    [Authorize]
    public ActionResult Upravit(int id)
    {

        var player = repo.Retrieve(id);
        var playerView = new PlayerViewModel(player);
        playerView.Users = new SelectList(repo.GetUsers(), "UserID", "UserName");
        return View(playerView);
    }
    [Authorize,HttpPost]
    public ActionResult Upravit(int id, PlayerViewModel playerView)
    {
        if (ModelState.IsValid)
        {
            playerView.Player.User = repo.GetUserByID(playerView.SelectedUserID);

            repo.Save(playerView.Player);
            return RedirectToAction("Podrobnosti", new { id = playerView.Player.PlayerID });
        }
        return View(playerView);
    }

and repo:

public User GetUserByID(int id)
        {
            return db.Users.Find(id);
        }

public int Save(Player player)
    {
        db.Entry(player).State = player.PlayerID == 0 ?
            EntityState.Added : EntityState.Modified;
        db.SaveChanges();
        return player.PlayerID;
    }

My problem is that I get in httppost valid viewmodel, I add User to player (it´s custom class and in player class it´s virtual User User) and in debug it looks everything ok (player contains right user) but nexttime when I in other view try to retrieve player it´s property with User is null. I checked it in database and there is no int in column with UserID. So maybe somebody can help me where is problem? Why it´s not saving?

Edit:
I added this code to my repository:

private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

and change in controller to this:

[Authorize,HttpPost]
        public ActionResult Upravit(int id, PlayerViewModel playerView)
        {
            if (ModelState.IsValid)
            {
                using (PlayerRepository playerRepository = new PlayerRepository())
                {
                    playerView.Player.User = playerRepository.GetUserByID(playerView.SelectedUserID);

                    playerRepository.Save(playerView.Player);
                    return RedirectToAction("Podrobnosti", new { id = playerView.Player.PlayerID });
                }
            }
            return View(playerView);
        }

but it didn´t help.

Edit2
My player and User classes:

 public class User
 {
    public User()
    {
    }
    public User(int userId, string userName, string firstName, string surname, string password, string email)
    {
        UserID = userId;
        UserName = userName;
        Name = firstName;
        Surname = surname;
        Password = password;
        Email = email;
        IsActivated = false;
    }

    [HiddenInput(DisplayValue = false)]
    public int UserID { get; set; }

    [Display(Name="Uživatelské jméno")]
    [Required(ErrorMessage = "Je potřeba vyplnit uživatelské jméno.")]
    [StringLength(20)]
    public string UserName { get; set; }

    public string Name { get; set; }

    public string Surname { get; set; }
    [Display(Name = "Heslo")]
    [DataType(DataType.Password)]
    [Required(ErrorMessage = "Je potřeba vyplnit heslo.")]
    [StringLength(30)]
    public string Password { get; set; }

    [Display(Name = "E-mail")]
    [DataType(DataType.EmailAddress)]
    [Required(ErrorMessage = "Je potřeba vyplnit Váš e-mail.")]
    [StringLength(30)]
    public string Email { get; set; }
    public IIdentity Identity { get; set; }

    public string ImagePath { get; set; }

    public virtual ICollection<Article> Articles { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual ICollection<Topic> Topics { get; set; }

    public int RoleId { get; set; }
    public virtual Role Role { get; set; }

    public bool IsActivated { get; set; }
    public string EmailKey { get; set; }
}
}

 public class Player
{
    [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; }
}

I added using with repo to every method in Controller and added Dispose method on controller and now I have problem with showing data. For example when I tried showing Player.User.UserName (I have manually added some users to players) It says that instance ObjectDataContext was dispose and cant be used.

  • 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-30T09:07:39+00:00Added an answer on May 30, 2026 at 9:07 am

    It seems it is necessary to dispose the ObjectContext; otherwise there are a different ObjectContext instances each time, you can run into problems when doing queries among objects of the contexts.

    Implement IDisposable for your Repository class: call the Dispose method for underlying ObjectContext class instance.

    Client code:

    using (PlayerRepository playerRepository = new PlayerRepository())
    {
        // Perform queries, etc.
    }
    

    Update

    To simplify disposing of the repository, just override Dispose method of Controller-derived class: call Dispose for the repository here. The ASP.NET MVC framework will automatically call Controller.Dispose() at the end of the request.

    Update

    It is necessary to define property called <entity>Id to define the relationship.

    public class Player
    {
        // Having a property called <entity>Id defines a relationship.
        public int UserId { get; set; }
        public virtual User User { get; set; }
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created this viewmodel: public class PlayerViewModel { PlayerRepository repo = new PlayerRepository(); public
I have something like this public class ViewModel { public List<Books> Test {get; set;}
This is my Viewmodel public class IndexViewModel { public string QuestionText { get; set;
If this is my view model: public class ViewModel{ public string SimpleProperty{get;set;} public SubViewModel
I have a ViewModel: public class Page { public int Id { get; set;
Suppose you have a viewModel: public class CreatePersonViewModel { [Required] public bool HasDeliveryAddress {get;set;}
I've got a Viewmodel that looks like this: public class Viewmodel { public int
I have a ViewModel. something like this public class ViewModel { public int Id
Given the following viewmodel: public class SomeViewModel { public bool IsA { get; set;
I have this ViewModel public class CustomerSuscribeViewModel : IValidatableObject { [DataMember(IsRequired = true)] [DataType(DataType.Text)]

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.