繁体   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