public ActionResult Create(IdentityDocument identityDocument)
 {
     using (var context = new DatabaseContext())
     {
         if (!ModelState.IsValid) return View(CreateTuple(context, identityDocument));
         try
         {
             context.Entry(identityDocument).State = EntityState.Added;
             context.SaveChanges();
             return RedirectToAction("WithIdentityDocuments", "Client", new { id = identityDocument.ClientId });
         }
         catch { return View(CreateTuple(context, identityDocument)); }
     }
 }
 /// <summary>GET action of the CREATE functionality</summary>
 /// <returns>View with new object</returns>
 public ActionResult Create(int clientId)
 {
     using (var context = new DatabaseContext())
     {
         var client = context.Clients
             .Include(x => x.Manager)
             .First(x => x.Id == clientId);
         var identityDocument = new IdentityDocument
         {
             ClientId = clientId
         };
         return View(new Tuple<Manager, Client, IdentityDocument>(client.Manager, client, identityDocument));
     }
 }
 /// <summary>Creates view model</summary>
 /// <returns>View model</returns>
 private static Tuple<Manager, Client, IdentityDocument> CreateTuple(
     DatabaseContext context, IdentityDocument identityDocument)
 {
     identityDocument.Client = context.Clients
         .Include(x => x.Manager)
         .First(x => x.Id == identityDocument.ClientId);
     return new Tuple<Manager, Client, IdentityDocument>(
         identityDocument.Client.Manager, identityDocument.Client, identityDocument);
 }
        /// <summary>Creates POC object from its properties</summary>
        /// <returns>POC object</returns>
        private static IdentityDocument ParseEntity(string id, string clientId,
            string type, string serie, string number, string issueDate, string expiryDate, string issuedBy,
            string dateOfBirth, string placeOfBirth, string identityId, string address, string isValid)
        {
            var identityDocument = new IdentityDocument();

            int no;
            if (!int.TryParse(id, out no))
                throw new Exception("Incorrect ID during instantiating of Identity Document");
            identityDocument.Id = no;

            int clientNo;
            if (!int.TryParse(clientId, out clientNo))
                throw new Exception("Incorrect Client ID during instantiating of Identity Document with ID = " + identityDocument.Id);
            identityDocument.ClientId = clientNo;

            int kind;
            if (!int.TryParse(type, out kind))
                throw new Exception("Incorrect Type during instantiating of Identity Document with ID = " + identityDocument.Id);
            switch (kind)
            {
                case 5: identityDocument.Type = IdentityDocumentType.RussianVisa; break;
                case 9: identityDocument.Type = IdentityDocumentType.RussianResidencePermit; break;
                case 13: identityDocument.Type = IdentityDocumentType.RussianPassport; break;
                case 19: identityDocument.Type = IdentityDocumentType.RussianForeignPassport; break;
                case 21: identityDocument.Type = IdentityDocumentType.RussianSeamanPassport; break;
                case 23: identityDocument.Type = IdentityDocumentType.EstonianVisa; break;
                case 27: identityDocument.Type = IdentityDocumentType.EstonianPassport; break;
                case 29: identityDocument.Type = IdentityDocumentType.EstonianSecondPassport; break;
                case 30: identityDocument.Type = IdentityDocumentType.EstonianAlienPassport; break;
                case 32: identityDocument.Type = IdentityDocumentType.EstonianIdCard; break;
                case 35: identityDocument.Type = IdentityDocumentType.EstonianResidencePermit; break;
                case 37: identityDocument.Type = IdentityDocumentType.EstonianSeamanPassport; break;
                case 39: identityDocument.Type = IdentityDocumentType.LatvianVisa; break;
                case 43: identityDocument.Type = IdentityDocumentType.LatvianPassport; break;
                case 51: identityDocument.Type = IdentityDocumentType.LatvianAlienPassport; break;
                case 55: identityDocument.Type = IdentityDocumentType.LatvianIdCard; break;
                case 59: identityDocument.Type = IdentityDocumentType.LatvianResidencePermit; break;
                case 61: identityDocument.Type = IdentityDocumentType.LatvianSeamanPassport; break;
                case 63: identityDocument.Type = IdentityDocumentType.LithuanianVisa; break;
                case 67: identityDocument.Type = IdentityDocumentType.LithuanianPassport; break;
                case 77: identityDocument.Type = IdentityDocumentType.LithuanianIdCard; break;
                case 81: identityDocument.Type = IdentityDocumentType.LithuanianResidencePermit; break;
                case 83: identityDocument.Type = IdentityDocumentType.LithuanianSeamanPassport; break;
                case 87: identityDocument.Type = IdentityDocumentType.FinnishVisa; break;
                case 97: identityDocument.Type = IdentityDocumentType.UsaVisa; break;
                case 999: identityDocument.Type = IdentityDocumentType.Other; break;
                default:
                    if (kind != 0)
                        throw new Exception("Incorrect Type during instantiating of Identity Document with ID = " + identityDocument.Id);
                    break;
            }

            identityDocument.Serie = WebUtility.HtmlDecode(
                (string.IsNullOrWhiteSpace(serie) ? string.Empty : serie).Trim());

            identityDocument.Number = WebUtility.HtmlDecode(
                (string.IsNullOrWhiteSpace(number) ? string.Empty : number).Trim());

            if (!string.IsNullOrWhiteSpace(issueDate))
            {
                DateTime issueDateInstance;
                if (!DateTime.TryParse(issueDate, out issueDateInstance))
                    throw new Exception("Incorrect Issue Date during instantiating of Identity Document with ID = " + identityDocument.Id);
                identityDocument.IssueDate = issueDateInstance;
            }

            if (!string.IsNullOrWhiteSpace(expiryDate))
            {
                DateTime expiryDateInstance;
                if (!DateTime.TryParse(expiryDate, out expiryDateInstance))
                {
                    if (expiryDate.StartsWith("29.02."))
                        expiryDate = expiryDate.Replace("29.02.", "28.02.");
                    if (expiryDate.StartsWith("31.09."))
                        expiryDate = expiryDate.Replace("31.09.", "30.09.");
                    if (!DateTime.TryParse(expiryDate, out expiryDateInstance))
                        throw new Exception("Incorrect Expiry Date during instantiating of Identity Document with ID = " + identityDocument.Id);
                }
                identityDocument.ExpiryDate = expiryDateInstance;
            }

            identityDocument.IssuedBy = WebUtility.HtmlDecode(
                (string.IsNullOrWhiteSpace(issuedBy) ? string.Empty : issuedBy).Trim());

            if (!string.IsNullOrWhiteSpace(dateOfBirth))
            {
                DateTime dateOfBirthInstance;
                if (!DateTime.TryParse(dateOfBirth, out dateOfBirthInstance))
                    throw new Exception("Incorrect Date Of Birth during instantiating of Identity Document with ID = " + identityDocument.Id);
                identityDocument.DateOfBirth = dateOfBirthInstance;
            }

            identityDocument.PlaceOfBirth = WebUtility.HtmlDecode(
                (string.IsNullOrWhiteSpace(placeOfBirth) ? string.Empty : placeOfBirth).Trim());

            identityDocument.IdentityId = WebUtility.HtmlDecode(
                (string.IsNullOrWhiteSpace(identityId) ? string.Empty : identityId).Trim());

            identityDocument.Address = WebUtility.HtmlDecode(
                (string.IsNullOrWhiteSpace(address) ? string.Empty : address).Trim());

            int isValidInstance;
            if (!int.TryParse(isValid, out isValidInstance))
                throw new Exception("Incorrect Is Valid during instantiating of Identity Document with ID = " + identityDocument.Id);
            switch (isValidInstance)
            {
                case 3: identityDocument.Status = IdentityDocumentStatus.Valid; break;
                case 5: identityDocument.Status = IdentityDocumentStatus.Outdated; break;
                default:
                    if (isValidInstance != 0)
                        throw new Exception("Incorrect Is Valid during instantiating of Identity Document with ID = " + identityDocument.Id);
                    break;
            }

            return identityDocument;
        }