如何将表单数据“发布”到数据库的2个表中

时间:2018-06-21 04:42:11

标签: c# razor controller asp.net-apicontroller

当我单击保存按钮时,我需要将html的表单数据添加到数据库的2个表中。我的问题是数据没有保存到任何表中

这是html部分:

$('#saveButton').on('click', function () {
    var collectedAccountName = $('#accountNameInput').val();
    var collectedDatepicker = $('#datepicker').val();
    var collectedRatePerHour = $('#ratePerHourInput').val();
    var collectedComment = $('#commentsInput').val();
    var collectedIsVisible = $('#togBtn').prop('checked');

        var webFormData = new WebFormData(collectedAccountName, collectedDatepicker, collectedRatePerHour,
            collectedComment, collectedIsVisible);

        var webFormDataInString = JSON.stringify(webFormData);

        $saveCustomerAccountHandler = jQuery.ajax({
            type: 'POST',
            url: '/API/CustomerAccounts/',
            dataType: 'json',
            contentType: 'application/json;',
            data: "'" + webFormDataInString + "'"
    })//end of ajax() call

这是API控制器HTTPPOST

[HttpPost]
    public IActionResult Post([FromBody]string value)
    {
        dynamic customerAccountNewInput = JsonConvert.DeserializeObject<dynamic>(value);
        CustomerAccount newCustomerAccount = new CustomerAccount();
        AccountRate newAccountRate = new AccountRate();
        try
        {
            newCustomerAccount.AccountName = customerAccountNewInput.AccountName.Value;
            newCustomerAccount.CreatedById = userId;
            newCustomerAccount.UpdatedById = userId;
            newCustomerAccount.Comments = customerAccountNewInput.Comment.Value;
            newCustomerAccount.IsVisible = customerAccountNewInput.IsVisible;
            newAccountRate.EffectiveStartDate = customerAccountNewInput.Datepicker.Value;
            newAccountRate.RatePerHour = customerAccountNewInput.RatePerHour.Value;
            Database.CustomerAccounts.Add(newCustomerAccount);
            Database.AccountRates.Add(newAccountRate);
            Database.SaveChanges();
        }
        catch (Exception e)
        {
          throw ex
        }

数据库

Picture of customer account database

Picture of account rate database

0 个答案:

没有答案