Domanda

I have a senario where a user wants to select his gender which is not a mandatory field. Here in my ui the gender is listed in dropdownlist which has two options Male and Female and has a optional label Gender. But the Gender is a enum in my project , so if the user did not select any gender a null value must be entered into the database, But unfortunately iam not able to do it due to some cast exception Can any body give me a help in resolving this?

I was using petapoco as my orm so my user entity looks like this

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Helpers;
using PetaPoco;
using StructureMap;

[TableName("User")]
[PrimaryKey("UserId")]
[ExplicitColumns]
public class User
{
    [Column("UserId")]
    public int Id { get; set; }

    [Column]
    [Required]
    public Guid Identifier { get; set; }

    [Column("UserStatusId")]
    [Required]
    public UserStatus UserStatus { get; set; }

    [Column]
    [Required]
    public int RoleId { get; set; }

    [Column]
    public int? SchoolId { get; set; }

    [Column]
    public int? LanguageId { get; set; }

    [Column("TitleId")]
    [Required]
    public Title Title { get; set; }

    [Column("GenderId")]
    [Required]
    public Gender? Gender { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string Username { get; set; }

    [StringLength(100)]
    public string Password { get; set; }

    [Column]
    [StringLength(100)]
    public string PasswordEncrypted { get; set; }

    [Column]
    [StringLength(10)]
    public string PasswordSalt { get; set; }

    [Column]
    [Required]
    [DataType(DataType.EmailAddress)]
    [StringLength(255)]
    public string Email { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string FirstName { get; set; }

    [Column]
    [Required]
    [StringLength(35)]
    public string LastName { get; set; }

    public string FullName { 
        get
        {
            return string.Concat(FirstName, " ", LastName);
        }
    }

    [Column]
    public DateTime? DateOfBirth { get; set; }

    [Column]
    public bool Subscribed { get; set; }

    [Column]
    public bool TermsAgreed { get; set; }

    [Column]
    public DateTime? TermsAgreedDate { get; set; }

    [Column]
    public DateTime CreatedDate { get; set; }

    [Column]
    public DateTime ModifiedDate { get; set; }

    [Column]
    public DateTime? LoginDate { get; set; }

    public Role Role { get; set; }

    public School School { get; set; }

    public Language Language { get; set; }
}

My gender enum is like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

public enum Gender
{
    /// <summary>
    /// Male
    /// </summary>
    [Description("Male")]
    Male = 1,

    /// <summary>
    /// Female
    /// </summary>
    [Description("Female")]
    Female
}

and my view model looks like this

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

/// <summary>
/// Model for edit view model user
/// </summary>
public class UserEditViewModel
{
    /// <summary>
    /// Gets or sets the User id
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// Gets or sets Title Id
    /// </summary>
    public int UserTitle { get; set; }

    /// <summary>
    /// Gets or sets the school Name
    /// </summary>
    public string Username { get; set; }

    /// <summary>
    /// Gets or sets the School first name
    /// </summary>
    public string FirstName { get; set; }

    /// <summary>
    /// Gets or sets the school last name
    /// </summary>
    public string LastName { get; set; }

    /// <summary>
    /// Gets or sets the school address line1
    /// </summary>
    public string Password { get; set; }

    /// <summary>
    /// Gets or sets the school address line2
    /// </summary>        
    public string ConfirmPassword { get; set; }

    /// <summary>
    /// Gets or sets the email address of the school
    /// </summary>
    public string Email { get; set; }

    /// <summary>
    /// Gets or sets the  created date
    /// </summary>
    public DateTime CreatedDate { get; set; }

    /// <summary>
    /// Gets or sets user status id
    /// </summary>
    public int UserStatus { get; set; }

    /// <summary>
    /// Gets or sets SchoolName
    /// </summary>
    public string SchoolDetails { get; set; }

    /// <summary>
    /// Gets or sets Language Id
    /// </summary>
    public int LanguageId { get; set; }

    /// <summary>
    /// Gets or sets Langauge list
    /// </summary>
    public IList<Language> Langauges { get; set; }

    /// <summary>
    /// Gets or sets RoleId
    /// </summary>        
    public int RoleId { get; set; }

    /// <summary>
    /// Gets or sets SchoolId
    /// </summary>
    public int SchoolId { get; set; }

    /// <summary>
    /// Gets or sets DateOfBirth
    /// </summary>
    public DateTime DateOfBirth { get; set; }

    /// <summary>
    /// Gets or sets Gender
    /// </summary>
    public int? Gender { get; set; }

    /// <summary>
    /// Gets UserGender list 
    /// </summary>
    public Dictionary<int, string> Genders
    {
        get
        {
            var listitems = new Dictionary<int, string>();
            foreach (Gender r in Enum.GetValues(typeof(Gender)))
            {
                listitems.Add((int)r, r.ToString());
            }

            return listitems;
        }
    }

    /// <summary>
    /// Gets Title list
    /// </summary>
    public Dictionary<int, string> Titles
    {
        get
        {
            var listitems = new Dictionary<int, string>();
            foreach (Title r in Enum.GetValues(typeof(Title)))
            {
                listitems.Add((int)r, r.ToString());
            }

            return listitems;
        }
    }

    /// <summary>
    /// Gets UserStatus list 
    /// </summary>
    public Dictionary<int, string> UserStatuses
    {
        get
        {
            var listitems = new Dictionary<int, string>();
            foreach (UserStatus r in Enum.GetValues(typeof(UserStatus)))
            {
                listitems.Add((int)r, r.ToString());
            }

            return listitems;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether Subscribed
    /// </summary>
    public bool Subscribed { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether TermsAgreed
    /// </summary>
    public bool TermsAgreed { get; set; }
}

And i was using automapper to map from User to UserEditviewModel and vice versa like this

   Mapper.CreateMap<User, UserEditViewModel>().ForMember(
           dest => dest.Langauges, opt => opt.Ignore()).ForMember(
           dest => dest.Password, opt => opt.Ignore()).ForMember(
           dest => dest.ConfirmPassword, opt => opt.Ignore()).ForMember(
           dest => dest.SchoolDetails, opt => opt.MapFrom(src =>
               (!string.IsNullOrEmpty(src.School.SchoolDetails.Region)) ?
               string.Concat(src.School.Name, ", ",
               src.School.SchoolDetails.City, ", ",
               src.School.SchoolDetails.Region, ", ",
               src.School.SchoolDetails.PostalCode) :
               string.Concat(src.School.Name, ", ",
               src.School.SchoolDetails.City, ", ",
               src.School.SchoolDetails.PostalCode))).ForMember(
          dest => dest.Titles, opt => opt.Ignore()).ForMember(
          dest => dest.UserStatuses, opt => opt.Ignore()).ForMember(
          dest => dest.UserTitle, opt => opt.MapFrom(src => src.Title)).ForMember(
          dest => dest.Genders, opt => opt.Ignore()).ForMember(
          dest => dest.Gender, opt => opt.MapFrom(src =>
              src.Gender == null ? (int?)null : (int)src.Gender));            

 Mapper.CreateMap<UserEditViewModel, User>().ForMember(
            dest => dest.Username, opt => opt.MapFrom(src =>
                HttpUtility.HtmlDecode(src.Username))).ForMember(
            dest => dest.FirstName, opt => opt.MapFrom(src =>
                HttpUtility.HtmlDecode(src.FirstName))).ForMember(
            dest => dest.LastName, opt => opt.MapFrom(src =>
                HttpUtility.HtmlDecode(src.LastName))).ForMember(
            dest => dest.Password, opt => opt.MapFrom(src =>
                HttpUtility.HtmlDecode(src.Password))).ForMember(
            dest => dest.Email, opt => opt.MapFrom(src =>
                HttpUtility.HtmlDecode(src.Email))).ForMember(
            dest => dest.Role, opt => opt.Ignore()).ForMember(
            dest => dest.RoleId, opt => opt.Ignore()).ForMember(
            dest => dest.ModifiedDate, opt => opt.Ignore()).ForMember(
            dest => dest.School, opt => opt.Ignore()).ForMember(
            dest => dest.Language, opt => opt.Ignore()).ForMember(
            dest => dest.LoginDate, opt => opt.Ignore()).ForMember(
            dest => dest.PasswordEncrypted, opt => opt.Ignore()).ForMember(
            dest => dest.PasswordSalt, opt => opt.Ignore()).ForMember(
            dest => dest.TermsAgreedDate, opt => opt.Ignore()).ForMember(
            dest => dest.Identifier, opt => opt.Ignore()).ForMember(
            dest => dest.Title, opt => opt.MapFrom(src => src.UserTitle)).ForMember(
            dest => dest.Gender, opt => opt.MapFrom( src =>
                src.Gender == null ? (Gender?)null : (Gender)src.Gender));

But after doing all this iam getting exception like this Invalid cast from 'System.Int32' to 'System.Nullable`1

Can anybody help?

È stato utile?

Soluzione

A rat's nest. But I would bet you have to change this code

(int)src.Gender

to

(int?)src.Gender

Or the full line (both places you do it)

dest => dest.Gender, opt => opt.MapFrom(src =>
          src.Gender == null ? (int?)null : (int?)src.Gender));  

Altri suggerimenti

public int? Gender { get; set; }

this line declares gender as nullable int

(int)src.Gender line tries to cast as int

For debug sake, ignore Gender mapping

dest => dest.Gender, opt => opt.Ignore()

But I think the problem is in SchoolId and LanguageId, you should map them properly.
All necessary information about fault mapping should be in stack trace.

Any particular reason why you're not using the Enum in your model?

I always do that, and have no problems, with the Flag Enum Model Binder MVC even handles flag enums without a bother!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top