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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:51:09+00:00 2026-05-17T23:51:09+00:00

I have two models for my form, a ViewModel going to it, and a

  • 0

I have two models for my form, a ViewModel going to it, and a ControlModel coming from it. The ControlModel has all the same field names and hierarchy, but all of the fields are a string data type.

How would you code AutoMapper to convert a string field to integer? I tried Int32.Parse(myString) but Int32 is not available within the expression (gives an error).

Mapper.CreateMap<SourceClass, DestinationClass>()
      .ForMember(dest => dest.myInteger, 
                  opt => opt.MapFrom(src => src.myString));

The types in the class and their corresponding conversion types:

string to int, int?, double, double?, DateTime, and bool

Additionally, is there any way to generalize mappings in a way that all integers in the target are parsed with that function? In other words, is there a way to create mappings for data types?

EDIT:

This looks promising:

AutoMapper.Mapper.CreateMap<string, int>()
          .ConvertUsing(src => Convert.ToInt32(src));

EDIT:
This post is really helpful

  • 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-17T23:51:09+00:00Added an answer on May 17, 2026 at 11:51 pm

    I ended up doing something like this:

    Mapper.CreateMap<string, int>().ConvertUsing<IntTypeConverter>();
    Mapper.CreateMap<string, int?>().ConvertUsing<NullIntTypeConverter>();
    Mapper.CreateMap<string, decimal?>().ConvertUsing<NullDecimalTypeConverter>();
    Mapper.CreateMap<string, decimal>().ConvertUsing<DecimalTypeConverter>();
    Mapper.CreateMap<string, bool?>().ConvertUsing<NullBooleanTypeConverter>();
    Mapper.CreateMap<string, bool>().ConvertUsing<BooleanTypeConverter>();
    Mapper.CreateMap<string, Int64?>().ConvertUsing<NullInt64TypeConverter>();
    Mapper.CreateMap<string, Int64>().ConvertUsing<Int64TypeConverter>();
    Mapper.CreateMap<string, DateTime?>().ConvertUsing<NullDateTimeTypeConverter>();
    Mapper.CreateMap<string, DateTime>().ConvertUsing<DateTimeTypeConverter>();
    
    Mapper.CreateMap<SourceClass, DestClass>();
    
    Mapper.Map(mySourceObject, myDestinationObject);
    

    And the classes it references (first draft):

    // TODO: Boil down to two with Generics if possible
    #region AutoMapTypeConverters
    // Automap type converter definitions for 
    // int, int?, decimal, decimal?, bool, bool?, Int64, Int64?, DateTime
    // Automapper string to int?
    private class NullIntTypeConverter : TypeConverter<string, int?>
    {   protected override int? ConvertCore(string source)
        {   if (source == null)
                return null;
            else
            {   int result;
                return Int32.TryParse(source, out result) ? (int?) result : null;
    }   }   }
    // Automapper string to int
    private class IntTypeConverter : TypeConverter<string, int>
    {   protected override int ConvertCore(string source)
        {   if (source == null)
                throw new MappingException("null string value cannot convert to non-nullable return type.");
            else
                return Int32.Parse(source); 
    }   }
    // Automapper string to decimal?
    private class NullDecimalTypeConverter : TypeConverter<string, decimal?>
    {   protected override decimal? ConvertCore(string source)
        {   if (source == null)
                return null;
            else
            {   decimal result;
                return Decimal.TryParse(source, out result) ? (decimal?) result : null;
    }   }   }
    // Automapper string to decimal
    private class DecimalTypeConverter : TypeConverter<string, decimal>
    {   protected override decimal ConvertCore(string source)
        {   if (source == null)
                throw new MappingException("null string value cannot convert to non-nullable return type.");
            else
                return Decimal.Parse(source); 
    }   }
    // Automapper string to bool?
    private class NullBooleanTypeConverter : TypeConverter<string, bool?>
    {   protected override bool? ConvertCore(string source)
        {   if (source == null)
                return null;
            else
            {   bool result;
                return Boolean.TryParse(source, out result) ? (bool?) result : null;
    }   }   }
    // Automapper string to bool
    private class BooleanTypeConverter : TypeConverter<string, bool>
    {   protected override bool ConvertCore(string source)
        {   if (source == null)
                throw new MappingException("null string value cannot convert to non-nullable return type.");
            else
                return Boolean.Parse(source); 
    }   }
    // Automapper string to Int64?
    private class NullInt64TypeConverter : TypeConverter<string, Int64?>
    {   protected override Int64? ConvertCore(string source)
        {   if (source == null)
                return null;
            else
            {   Int64 result;
                return Int64.TryParse(source, out result) ? (Int64?)result : null;
    }   }   }
    // Automapper string to Int64
    private class Int64TypeConverter : TypeConverter<string, Int64>
    {   protected override Int64 ConvertCore(string source)
        {   if (source == null)
                throw new MappingException("null string value cannot convert to non-nullable return type.");
            else
                return Int64.Parse(source); 
    }   }
    // Automapper string to DateTime?
    // In our case, the datetime will be a JSON2.org datetime
    // Example: "/Date(1288296203190)/"
    private class NullDateTimeTypeConverter : TypeConverter<string, DateTime?>
    {   protected override DateTime? ConvertCore(string source)
        {   if (source == null)
                return null;
            else
            {   DateTime result;
                return DateTime.TryParse(source, out result) ? (DateTime?) result : null;
    }   }   }
    // Automapper string to DateTime
    private class DateTimeTypeConverter : TypeConverter<string, DateTime>
    {   protected override DateTime ConvertCore(string source)
        {   if (source == null)
                throw new MappingException("null string value cannot convert to non-nullable return type.");
            else
                return DateTime.Parse(source); 
    }   }
    #endregion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that calls on two separate models. My validation works correctly
I have two Models, Programme and Event, a programme has many events. I need
I have a simple form with two fields and an optional field which is
I have a view that has two buttons(First and Second) and one hidden field.
I have an insurance entry form that has contact information for two people. I
I have two models: Post and Tag. The Post model has an attr_accessor called
I have two models Project which has has_and_belongs_to_many :users and Users which has has_and_belongs_to_many
I have an action called Messages which has two forms. The first form posts
I am using MVC3, EF Model first. I have a form with two DropDownList
I have two models, Listing and Invitation, associated with has_and_belongs_to_many. I am looking at

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.