簡體   English   中英

ASP.net 核心 Web API post 數組作為屬性

[英]ASP.net core Web API post array as a property

我使用一個數組來存儲問題標簽,但是當我從 Chrome POST 數據時,我沒有在 TagId 數組上收到任何數據。 其他屬性正常,我收到它們 onlu TagId 為null這是我的代碼:

問題.cs

public partial class Question 
{

    [Key]
    public int Id { get; set; }

    ...

    [Display(Name = "Tags")]
    [NotMapped]
    public int[] TagId
    {
      get
      {
          Some Code Here
      }

      set
      {
        Tag = "";
        if(value==null)
          return;
        foreach (var i in value)
        {
          Tag = i + ",";
        }

        Tag = Tag.Trim(',');
      }
    }

    [ScaffoldColumn(false)]
    public string Tag { get; set; }
    ...
}

這是帶有虛擬數據Chrome 帖子數據的chrome 帖子,我在 asp.net 帖子數據中收到 null: Visual Studio Trace on Data receive

這是我的控制器郵政編碼:

QuestionsController.cs

[Route("adminapi/[controller]")]
[ApiController]
public class QuestionsController : BaseRControllerWithFile<Question,QuestionAdminRepository>
{
...
  public override async Task<ActionResult<Question>> Post(Question entity)
  {
    return await base.Post(entity);
  }
...
}

將問題類更改為:

public class Question
{
    private int[] tagId;

    private string tag;

    [Key]
    public int Id { get; set; }

    [Display(Name = "Tags")]
    [NotMapped]
    public int[] TagId
    {
        get => tagId;
        set
        {
            tagId = value;

            tag = "";
            if (value == null)
                return;
            foreach (var i in tagId)
            {
                tag += i + ",";
            }

            tag = tag.Trim(',');
        }
    }

    [ScaffoldColumn(false)]
    public string Tag => tag;
}

或者正如@Paul 所說; 你可以使用加入:

tag = value != null ? string.Join(',', tagId) : null;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM