Sam Khan Sam Khan - 1 month ago 19
C# Question

Cannot implicitly convert type?

I have a error on

unauthenticatedEnquiryViewModel.CCommmunication;
mentioning that cannot implicitly convert type 'PCSS.Portal.Models.UnauthenticatedEnquirtyViewModels.Communcation?' to an int. An explit conversion exists (Are you missing a cast?)

if (ModelState.IsValid)
{
var unauthenticatedFormSubmission = new PCSS.Portal.DataAccess.Models.UnauthenticatedFormSubmission();

unauthenticatedFormSubmission.FirstName = unauthenticatedEnquiryViewModel.FirstName;
unauthenticatedFormSubmission.LastName = unauthenticatedEnquiryViewModel.LastName;
unauthenticatedFormSubmission.PreferredContactMethod = unauthenticatedEnquiryViewModel.CCommmunication; //error


My serializable class

[Serializable]
public class UnauthenticatedFormSubmission
{
public string FirstName;
public string LastName;
public string Telephone1;
public int PreferredContactMethod;


My model

public class UnauthenticatedEnquiryViewModel : IValidatableObject
{
[StringLength(35, ErrorMessage = "Name cannot be longer than 35 characters.")]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Please use characters only.")]
[Required(ErrorMessage = "Please fill in the required field.")]
public string FirstName { get; set; }

[StringLength(35, ErrorMessage = "Last name cannot be longer than 35 characters.")]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Please use characters only.")]
[Required(ErrorMessage = "Please fill in the required field.")]
public string LastName { get; set; }

[StringLength(100, ErrorMessage = "Email cannot be longer than 35 characters.")]
[DataType(DataType.EmailAddress)]
[RegularExpression("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$", ErrorMessage = "Must be a valid email i.e. jsmith@example.org")]
public string Email { get; set; }

[Required(ErrorMessage = "Please select preferred way of communication option")]
public Commmunication? CCommmunication
{ get; set; }

public enum Commmunication
{
[Display(Name = "Email", Order = 0)]
Email,

[Display(Name = "Mobile telephone", Order = 1)]
TelephoneNo,

[Display(Name = "Alternative telephone", Order = 2)]
TelephoneNoAlternative
}

Answer

Sam just do

unauthenticatedFormSubmission.PreferredContactMethod = (int)unauthenticatedEnquiryViewModel.CCommmunication;
Comments