knockout.js 来自前端的Json数据未通过后端使用Knockout和MVC插入数据库

kokeuurv  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(134)

我正在做一个项目,我使用Knockout和MVC将一些东西从本地存储保存到我的数据库中。我遇到的问题是,我在控制器中指定的数据(我需要发送到后端)被插入,但我从前端提取的数据却没有被插入。下面是我的前端api.js:

function Save(data){
        // var promise = appsecurity.postApiCall(root+'/Save', ko.toJSON(data));
        return appsecurity.putApiCall(root+'/Save', ko.toJSON(data));
    }

项目文件. js

self.changeButtonText = function(){
         self.ProcurementbuttonText(!self.ProcurementbuttonText())
         self.ElementData = ko.observable(localStorage.getItem('ElementDataWidget'));
         self.scorecardIDLocalStorage = ko.observable(localStorage.getItem('scorecardId'));
         self.AllData = ko.observableArray([]);
         self.AllData.push(self.scorecardIDLocalStorage,self.ElementData);

         var JSONData = ko.toJSON(self.AllData);
         PreferentialProcurementDashboardApi.Save(JSONData);
         console.log((JSONData));

        }

因此,正如您所看到的,我正在记录我的JSONData,以确保我确实从本地存储中获取了数据,因此它确实显示在我的控制台中。
Controller.cs:

[HttpPut]
    [Route("Save")]
    [ValidateModel]
    //[TrackActivity(section: "Dashboard", actionType: UserActivityActionType.Update, actionDescription: "Create/Update dashboard View Entry")]

    public IHttpActionResult Save(DashboardConfigEditViewModel dashboardConfigEditViewModel)
    {
        //var databaseDetails = _prefentialDashboardConfigService.GetById(dashboardConfigEditViewModel.DashboardId);
        //if (databaseDetails != null)
        //{
        //    return AccessDenied();
        //}

        //int id = SaveDashboardConfigEditViewModel(dashboardConfigEditViewModel);
        //return Ok(id);

        dashboardConfigEditViewModel.DateCreated = DateTime.Now;
        dashboardConfigEditViewModel.UserId = this.GetUserId();
        _prefentialDashboardConfigService.Create(PreferentialProcurmentConfigViewModelMapper.MapToDomain(dashboardConfigEditViewModel));
        return Ok();
    }

我的Map器.cs:

public static PrefentialDashboardConfig MapToDomain(DashboardConfigEditViewModel dashboardconfigeditviewmodel)
    {
        var config = new PrefentialDashboardConfig();
        config.DashboardId = dashboardconfigeditviewmodel.DashboardId;
        config.ScorecardId = dashboardconfigeditviewmodel.ScorecardId;
        config.UserId = dashboardconfigeditviewmodel.UserId;
        config.DateCreate = dashboardconfigeditviewmodel.DateCreated;
        config.DashboardConfig = dashboardconfigeditviewmodel.DashboardConfig;

        return config;
    }

我的收件人:

public class DashboardConfigEditViewModel
{

    //public DashboardConfigEditViewModel()
    //{

    //}

    public int DashboardId { get; set; }

    public Guid ScorecardId { get; set; }

    public Guid UserId { get; set; }

    public DateTime DateCreated { get; set; }

    public string DashboardConfig { get; set; }

}

这是我的数据库中的字段:

Create Table [data].PrefentialDashboardConfig(
DashboardId int IDENTITY(1,1) PRIMARY KEY,
ScorecardId uniqueidentifier,
UserId uniqueidentifier,
DateCreate DateTime,
DashboardConfig varchar(255)

)

现在,如果您看一下我的控制器,我在后端设置日期和用户ID,它们将插入到我的数据库中,但我从前端获得的记分卡ID和配置并不会一直解析到后端

2ic8powd

2ic8powd1#

JSON中的属性名称(我们将其发送到服务器)应与MVC viewmodel类的属性名称(保存ActionResult方法的参数)匹配。

**选项1:**如果您使用ko.toJSON方法,则应使用如下所示的模型;

var dashboardConfigEditViewModel = {
        ScorecardId:ko.observable(localStorage.getItem('scorecardId')),
        DashboardConfig:ko.observable(localStorage.getItem('ElementDataWidget'))
};

var JSONData = ko.toJSON(dashboardConfigEditViewModel);
PreferentialProcurementDashboardApi.Save(JSONData);

**选项2:**如果您不想使用ko.toJSON方法,可以使用下面的选项。

var dashboardConfigEditViewModel = {
        'ScorecardId':ko.observable(localStorage.getItem('scorecardId'))(),
        'DashboardConfig':ko.observable(localStorage.getItem('ElementDataWidget'))()
};

PreferentialProcurementDashboardApi.Save(dashboardConfigEditViewModel);

注意:在发送到服务器之前,请确保使用JSON.stringify()将JSON转换为字符串。

相关问题