1

我正在编写一个应用程序,该应用程序接受用户输入来创建连接组织的 xero 联系人的新联系人。我设法做到了,当您单击创建联系人时,它会显示我的联系表格 [联系表格] [1]

我已经使用了一些代码并添加了一些。API 调用发布主要人员和联系人的数据。但是没有保存其他详细信息,例如地址和电话,请您帮忙。我对开发很陌生。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using KompleteMe.Models;
using KompleteMe.Utilities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Xero.NetStandard.OAuth2.Api;
using Xero.NetStandard.OAuth2.Client;
using Xero.NetStandard.OAuth2.Config;
using Xero.NetStandard.OAuth2.Model.Accounting;
using Xero.NetStandard.OAuth2.Token;

namespace KompleteMe.Controllers
{
    public class ContactsInfo : Controller
    {
        private readonly ILogger<XeroAuthorizationController> _logger;
        private readonly IOptions<XeroConfiguration> XeroConfig;
        private readonly IHttpClientFactory httpClientFactory;

        public ContactsInfo(IOptions<XeroConfiguration> XeroConfig, IHttpClientFactory httpClientFactory, ILogger<XeroAuthorizationController> logger)
        {
            _logger = logger;
            this.XeroConfig = XeroConfig;
            this.httpClientFactory = httpClientFactory;
        }

        // GET: /ContactsInfo/
        public ActionResult Index()
        {

            return View();
        }

        // GET: /ContactsInfo#Create
        [HttpGet]
        public IActionResult Create()
        {
            var contact = new ContactModel();
            return View(contact);
        }

        // POST: /ContactsInfo#Create
        [HttpPost]
        public async Task<ActionResult> Create(ContactModel model)
        {
            var xeroToken = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);
                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();
            var contactp = new ContactPerson();
            var contact = new Contact();
            var phone = new Phone();
            var address = new Address();
            contact.Name = model.Name;
            contact.FirstName=model.FirstName;
            contact.LastName = model.LastName;
            contact.EmailAddress = model.EmailAddress;
            //Contact person .
            contactp.FirstName = model.PersonFirstName;
            contactp.LastName = model.PersonLastName;
            contactp.EmailAddress = model.PersonEmailAddress;
            phone.PhoneNumber = model.PhoneNumber;
            phone.PhoneCountryCode = model.PhoneCountryCode;
            address.AddressLine1 = model.AddressLine1;
            address.City = model.City;
            address.PostalCode = model.PostalCode;
           // contact.Website = model.Website;
            contact.ContactPersons  = new List<ContactPerson>();
            contact.ContactPersons.Add(contactp);
            contact.TaxNumber = model.TaxNumber;
            contact.Phones = new List<Phone>();
            contact.Phones.Add(phone);

            contact.Addresses = new List<Address>();
            contact.Addresses.Add(address);

            var contacts = new Contacts();
            
            contacts._Contacts = new List<Contact>() { contact };

            var AccountingApi = new AccountingApi();
            var response = await AccountingApi.CreateContactsAsync(accessToken, xeroTenantId, contacts);

            return RedirectToAction("Index", "ContactsInfo");
        }
    }
}



  using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Threading.Tasks;
    using RestSharp.Validation;
    
    namespace KompleteMe.Models
    {
        public class ContactModel
        {
          
            [Required(ErrorMessage = "Name is required")]
            public string Name { get; set; }
            [Required(ErrorMessage = "Firstname is required")]
            public string FirstName { get; set; }
            [Required(ErrorMessage ="Lastname is required")]
            public string LastName { get; set; }
            [Required(ErrorMessage = "Email is required")]
            public string EmailAddress { get; set; }
            [Required(ErrorMessage = "Firstname is required")]
            public string PersonFirstName{ get; set; }
            [Required(ErrorMessage = "Lasttname is required")]
            public string PersonLastName { get; set; }
            [Required(ErrorMessage = "Email is required")]
            public string PersonEmailAddress { get; set; }
            [Required(ErrorMessage = "tax is required")]
            public string TaxNumber { get; set; }
            [Required(ErrorMessage = "phone is required")]
            public string PhoneNumber { get; set; }
    
            [Required(ErrorMessage = "phonecode is required")]
            public string PhoneCountryCode { get; set; }
            [Required(ErrorMessage = "addressline1 is required")]
            public string AddressLine1 { get; set; }
            [Required(ErrorMessage = "city is required")]
            public string City { get; set; }
            [Required(ErrorMessage = "postalcode is required")]
            public string PostalCode { get; set; }
            public string Website { get; set; }
        }
    }

 

@model ContactModel
@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Contact</h4>
<hr />
<form asp-action="Create">

    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <label asp-for="Name" class="control-label">Company Name </label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label asp-for="EmailAddress" class="control-label">Email Address </label>
                <input asp-for="EmailAddress" class="form-control" />
                <span asp-validation-for="EmailAddress" class="text-danger"></span>
            </div>
        </div>
    </div>
    <h4>Contact Person </h4>

    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <label asp-for="PersonFirstName" class="control-label">First Name</label>
                <input asp-for="PersonFirstName" class="form-control" />
                <span asp-validation-for="PersonFirstName" class="text-danger"></span>


                <label asp-for="PersonEmailAddress" class="control-label">Email Address</label>
                <input asp-for="PersonEmailAddress" class="form-control" />
                <span asp-validation-for="PersonEmailAddress" class="text-danger"></span>

                <label asp-for="PhoneNumber" class="control-label"> Phone Number</label>
                <input asp-for="PhoneNumber" class="form-control" />
                <span asp-validation-for="PhoneNumber" class="text-danger"></span>



                <label asp-for="PhoneCountryCode" class="control-label">Phone Country Code </label>
                <input asp-for="PhoneCountryCode" class="form-control" />
                <span asp-validation-for="PhoneCountryCode" class="text-danger"></span>

                <label asp-for="TaxNumber" class="control-label">Vat Number </label>
                <input asp-for="TaxNumber" class="form-control" />
                <span asp-validation-for="TaxNumber" class="text-danger"></span>

            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label asp-for="PersonLastName" class="control-label">Last Name </label>
                <input asp-for="PersonLastName" class="form-control" />
                <span asp-validation-for="PersonLastName" class="text-danger"></span>
                <label asp-for="AddressLine1" class="control-label"> Street Address </label>
                <input asp-for="AddressLine1" class="form-control" />
                <span asp-validation-for="AddressLine1" class="text-danger"></span>

                <label asp-for="City" class="control-label">City</label>
                <input asp-for="City" class="form-control" />
                <span asp-validation-for="City" class="text-danger"></span>

                <label asp-for="PostalCode" class="control-label">Postal Code</label>
                <input asp-for="PostalCode" class="form-control" />
                <span asp-validation-for="PostalCode" class="text-danger"></span>
                <label asp-for="Website" class="control-label">Website</label>
                <input asp-for="Website" class="form-control" />
                <span asp-validation-for="Website" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>

        </div>
    </div>

















</form>




@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
4

0 回答 0