简体   繁体   English

将JavaScript对象传递给C#

[英]Passing JavaScript object to C#

I am trying to create a new row in a table I have hosted in Azure SQL Database. 我正在尝试在Azure SQL数据库中托管的表中创建新行。 My front end is AngularJS with C# in .NET as the back end. 我的前端是AngularJS,后端使用.NET中的C#。 Here is my code from the front end passing the object: 这是我从前端传递对象的代码:

var insertTicket = function (newTicket) {
    return $http.post("http://localhost:50412/api/tickets", JSON.stringify(newTicket))
                .then(function (response) {
                    console.log(response);
                    console.log("Insert Successful");
                    return;
                });

And here is my backend code that receives the data and tries to add to the database: 这是我的后端代码,用于接收数据并尝试添加到数据库中:

[Route("api/tickets")]
public HttpResponseMessage Post(Ticket t)
{
    TicketsRepository.InsertTicket(t);
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

In TicketRepisitory: 在TicketRepisitory中:

public static void InsertTicket(Ticket tick)
{
    var maxID = (from ticket in dataContext.Tickets
                 select ticket.id).Max();
    var tick = new Ticket();
    tick.id = maxID + 1;

    dataContext.Tickets.Add(tick);
    dataContext.SaveChanges();
}

And Here is my Ticket class: 这是我的票务类:

public partial class Ticket
{
    //Properties
    public int id { get; set; }
    public string title { get; set; }
    public string customer { get; set; }
    public string barcode { get; set; }
    public string assignedTo { get; set; }
    public string category { get; set; }
    public string importance { get; set; }
    public Nullable<System.DateTime> openDate { get; set; }
    public Nullable<System.DateTime> dueDate { get; set; }
    public Nullable<System.DateTime> closedDate { get; set; }
    public string comments { get; set; }
    public string condition { get; set; }
    public Nullable<int> workHours { get; set; }

    //Relationships

    public Employee Employee { get; set; }
    public Employee Employee1 { get; set; }
    public Equipment Equipment { get; set; }

}

I think the issue is with Post() expecting a ticket object. 我认为问题出在Post()希望有票证对象。 I have tried searching for how to receive JSON data and use that for Ticket, but with out much luck. 我尝试搜索如何接收JSON数据并将其用于Ticket,但运气不佳。
My Problem is that I can not create a new row. 我的问题是我无法创建新行。 No changes are reflected in my database. 我的数据库没有任何变化。

You don't need to JSON.stringify your object when POSTing data with $http in AngularJS, just pass the object itself as the second parameter, like this: 在AngularJS中使用$http数据时,您无需对对象进行JSON.stringify ,只需将对象本身作为第二个参数传递,如下所示:

var insertTicket = function (newTicket) {
return $http.post("http://localhost:50412/api/tickets", newTicket)
            .then(function (response) {
                console.log(response);
                console.log("Insert Successful");
                return;
            });

First there is no need to call JSON.stringify() method on newticket javascript object for second paramter of $http.post() method. 首先,不需要为$ http.post()方法的第二个参数在newticket javascript对象上调用JSON.stringify()方法。

Then in the web api method write a parameter of type JObject with the name newTicket to receive the posted object, and use generic version of ToObject method to convert posted data to desired type. 然后,在Web api方法中,使用名称newTicket编写类型为JObject的参数以接收发布的对象,并使用ToObject方法的通用版本将发布的数据转换为所需的类型。 Don't forget to use [FromBody] attribute for the method parameter. 不要忘记为方法参数使用[FromBody]属性。 the code for webapi looks like this: webapi的代码如下所示:

[Route("api/tickets")]
public HttpResponseMessage Post([FromBody]JObject newTicket)
{
    var t = newTicket.ToObject<Ticket>();
    TicketsRepository.InsertTicket(t);
    HttpResponseMessage = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM