【问题标题】:Post (using fetch) a List of object inside of general object JSX to ASP.Net Core API将通用对象 JSX 中的对象列表发布(使用 fetch)到 ASP.Net Core API
【发布时间】:2020-12-05 11:29:11
【问题描述】:

我正在使用 ReactJS 和 ASP.Net Web API

这是模型:

  public class Skill
{
  public int Id { get; set; }
  public string Description { get; set; }
}

public class RegisterProject
{
  public class Request
  {
    public string Objective { get; set; }
    public string Description { get; set; }
    public long UserId { get; set; }
    public int CurrencyId { get; set; }
    public int BudgetId { get; set; }

    public List<Skill> Skills = new List<Skill>();
  }

  public class Response : Models.Common.Generic

控制器:

   [HttpPost]
    [Route("[action]")]
    [EnableCors("AllowAllOrigin")]
    public ActionResult<Models.Main.RegisterProject.Response> postRegisterProject([FromBody] Models.Main.RegisterProject.Request registerProjectRequest)

在 React JS 中

常量句柄提交 = (e) => { console.log(formPublishProject);

fetch(global.config.url + "Project/postRegisterProject/", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
  },
  body: JSON.stringify({
    Objective: "a",
    Description: "b",
    UserId: 1,
    CurrencyId: 1,
    BudgetId: 1,
    Skills: [
      { Id: 1, Description: "ReactJS" },
      { Id: 2, Description: "Html" },
    ],
  }),
})
  .then(function (response) {
    response.json().then(function (data) {
      console.log("llego aqui");
      console.log(data);
    });
  })
  .catch(function (err) {
    console.log("Fetch Error :-S", err);
  });

};

在控制器下面的调试图我没有收到技能列表,不知道是我的技能定义错误还是有其他问题?

【问题讨论】:

  • "public List Skills { get; set; }" 我想应该这样做

标签: c# reactjs asp.net-core asp.net-web-api arraylist


【解决方案1】:

请像这样改变技能

public List<Skill> Skills { get; set; }

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 2019-01-29
    • 2019-08-06
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多