简体   繁体   English

使类属性成为可选的WebAPI MVC5

[英]Making class property optional WebAPI MVC5

I am using MVC5 WebAPI and i have a class as follows: 我正在使用MVC5 WebAPI,并且具有如下类:

public string Name { get; set; }
public string Sub { get; set; }
public List<Actions> Actions { get; set; }

( Actions is another custom class) Actions是另一个自定义类)

When client makes an API call they have to send the Actions property as null. 客户端进行API调用时,他们必须将Actions属性发送为null。 Is there a way that the client may not send the actions property at all? 客户端是否可以完全不发送action属性?

How could this be done? 怎么办呢?

Thanks in advance. 提前致谢。

I think this public List Actions { get; 我认为这个公开的List Actions {get; set; 组; } is optional by default. }默认是可选的。 if user/client don't specify it will be null. 如果用户/客户端未指定,则为null。 Have you added any required field validations to it? 您是否已添加任何必需的字段验证?

If the client doesn't post actions. 如果客户未发布操作。 Then Actions at the time of model binding would be null . 然后,模型绑定时的Actions将为null So there isn't any extra work from your side. 因此,您身边没有任何额外的工作。 In other words, if you post only Name and Sub , then Actions would be null. 换句话说,如果仅发布NameSub ,则Actions为null。

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

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