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

  • Home
  • SEARCH
  • 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 938431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:38:34+00:00 2026-05-15T21:38:34+00:00

I’m building an Asp.net MVC 2 application. I have an entity called Team that

  • 0

I’m building an Asp.net MVC 2 application.

I have an entity called Team that is mapped via public properties to two other entities called Gender and Grade.

public class Team
{
    public virtual int Id { get; private set; } 
    public virtual string CoachesName { get; set; } 
    public virtual string PrimaryPhone { get; set; } 
    public virtual string SecondaryPhone { get; set; }
    public virtual string EmailAddress { get; set; } 
    public virtual Grade Grade { get; set; } 
    public virtual  Gender Gender { get; set; } 
}

I have a ViewModel that looks like this.

public class TeamFormViewModel
{

    public TeamFormViewModel()
    {
        Team = new Team();
        Grade = new SelectList((new Repository<Grade>()).GetList(),"ID", "Name",Team.Grade);
        Gender = new SelectList((new Repository<Gender>()).GetList(), "ID", "Name", Team.Gender);
    }

    public Team Team { get; set; }
    public virtual SelectList Grade { get; set; }
    public virtual SelectList Gender { get; set; }
}

My form renders as I would expect. When I debug the Create method I see that the Gender and Grade properties are NULL on my Team object.

    [HttpPost, Authorize]
    public ActionResult Create(Team team)
    {
        try
        {
            if (ModelState.IsValid)
            {
                (new Repository<Team>()).Save(team);

            }
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

What am I doing wrong?

Thanks,
Eric

  • 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-15T21:38:35+00:00Added an answer on May 15, 2026 at 9:38 pm

    I recommend that you post and bind back to a view model class rather than your entity class. Create an extension method for your view model class that will return your entity class. Here’s some working code:

    public class Team
    {
       public virtual int Id { get; set; }
       public virtual string CoachesName { get; set; }
       public virtual string PrimaryPhone { get; set; }
       public virtual string SecondaryPhone { get; set; }
       public virtual string EmailAddress { get; set; }
       public virtual Grade Grade { get; set; }
       public virtual Gender Gender { get; set; }
    }
    
    public class Grade
    {
       public virtual int Id { get; set; }
       public virtual string Name { get; set; }
    }
    
    public class Gender
    {
       public virtual int Id { get; set; }
       public virtual string Name { get; set; }
    }
    
    public class TeamFormViewModel
    {
       public TeamFormViewModel()
       {
          var gradeList = (new Repository<Grade>()).GetList();
          var genderList = (new Repository<Gender>()).GetList();
          GradeList = new SelectList(gradeList, "Id", "Name");
          GenderList = new SelectList(genderList, "Id", "Name");
       }
    
       [HiddenInput(DisplayValue = false)]
       public int Id { get; set; }
    
       [DisplayName("Coach Name")]
       [Required]
       public string CoachesName { get; set; }
    
       [DisplayName("Primary Phone")]
       [DataType(DataType.PhoneNumber)]
       [Required]
       public string PrimaryPhone { get; set; }
    
       [DisplayName("Secondary Phone")]
       [DataType(DataType.PhoneNumber)]
       public string SecondaryPhone { get; set; }
    
       [DisplayName("Email Address")]
       [DataType(DataType.EmailAddress)]
       [Required]
       public string EmailAddress { get; set; }
    
       [DisplayName("Grade")]
       [Range(1, 5)]
       public int SelectedGradeId { get; set; }
    
       [DisplayName("Gender")]
       [Range(1, 5)]
       public int SelectedGenderId { get; set; }
    
       private int selectedGradeId = 0;
       private int selectedGenderId = 0;
    
       public SelectList GradeList { get; set; }
       public SelectList GenderList { get; set; }
    }
    
    public static class TeamExtensions
    {
       public static Team ToTeam(this TeamFormViewModel viewModel)
       {
          return new Team
          {
             Id = viewModel.Id,
             CoachesName = viewModel.CoachesName,
             PrimaryPhone = viewModel.PrimaryPhone,
             SecondaryPhone = viewModel.SecondaryPhone,
             EmailAddress = viewModel.EmailAddress,
             Grade = (new Repository<Grade>())
                .GetList()
                .Where(x => x.Id == viewModel.SelectedGradeId)
                .Single(),
             Gender = (new Repository<Gender>())
                .GetList()
                .Where(x => x.Id == viewModel.SelectedGradeId)
                .Single()
          };
       }
    
       public static TeamFormViewModel ToTeamFormViewModel(this Team team)
       {
          return new TeamFormViewModel
          {
             Id = team.Id,
             CoachesName = team.CoachesName,
             PrimaryPhone = team.PrimaryPhone,
             SecondaryPhone = team.SecondaryPhone,
             EmailAddress = team.EmailAddress,
             SelectedGradeId = team.Grade.Id,
             SelectedGenderId = team.Gender.Id
          };
       }
    }
    
    public class TeamController : Controller
    {
       public ActionResult Create()
       {
          var viewModel = new TeamFormViewModel();
          return View(viewModel);
       }
    
       [HttpPost]
       public ActionResult Create(TeamFormViewModel viewModel)
       {
          if (ModelState.IsValid)
          {
             (new Repository<Team>())
                .Save(viewModel.ToTeam());
          }
          return View(viewModel);
       }
    }
    

    And finally, the view:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Stack1.Models.TeamFormViewModel>" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
       <title>Create</title>
       <script type="text/javascript" src="/Scripts/jquery-1.4.1.js"></script>
       <script type="text/javascript" src="/Scripts/MicrosoftAjax.js"></script>
       <script type="text/javascript" src="/Scripts/MicrosoftMvcValidation.js"></script>
    </head>
    <body>
       <% Html.EnableClientValidation(); %>
       <% using (Html.BeginForm()) { %>
       <%= Html.ValidationSummary() %>
       <fieldset>
          <legend>Fields</legend>
          <%= Html.LabelFor(x => x.CoachesName) %>
          <p>
             <%= Html.TextBoxFor(x => x.CoachesName) %>
             <%= Html.ValidationMessageFor(x => x.CoachesName) %>
          </p>
    
          <%= Html.LabelFor(x => x.PrimaryPhone)%>
          <p>
             <%= Html.EditorFor(x => x.PrimaryPhone) %>
             <%= Html.ValidationMessageFor(x => x.PrimaryPhone)%>
          </p>
    
          <%= Html.LabelFor(x => x.SecondaryPhone)%>
          <p>
             <%= Html.EditorFor(x => x.SecondaryPhone) %>
             <%= Html.ValidationMessageFor(x => x.SecondaryPhone)%>
          </p>
    
          <%= Html.LabelFor(x => x.EmailAddress)%>
          <p>
             <%= Html.EditorFor(x => x.EmailAddress) %>
             <%= Html.ValidationMessageFor(x => x.EmailAddress)%>
          </p>
    
          <%= Html.LabelFor(x => x.SelectedGradeId)%>
          <p>
             <%= Html.DropDownListFor(x => x.SelectedGradeId, Model.GradeList) %>
             <%= Html.ValidationMessageFor(x => x.SelectedGradeId)%>
          </p>
    
          <%= Html.LabelFor(x => x.SelectedGenderId)%>
          <p>
             <%= Html.DropDownListFor(x => x.SelectedGenderId, Model.GenderList) %>
             <%= Html.ValidationMessageFor(x => x.SelectedGenderId)%>
          </p>
          <p>
             <%= Html.HiddenFor(x => x.Id) %>
             <input type="submit" value="Save" />
          </p>
       </fieldset>
       <% } %>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.