c# - 具有多个模型的 RestPost
问题描述
我只是用 django-rest-framework 创建我的 RestApi,我的模型是嵌套的。我的意思是,用一个表单创建两个模型,它使用 json 数据,但我需要一点 C# 帮助,因为它是我第一次使用它,所以此刻我有我的 RestService:
public async Task SaveTodoItemAsync(DailyHours item)
{
var tokenStorage = await SecureStorage.GetAsync("token");
var authHeader = new AuthenticationHeaderValue("token", tokenStorage);
Uri uri = new Uri(string.Format(Constants.DailyHoursPostWebServiceUrl, string.Empty));
try
{
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
client.DefaultRequestHeaders.Authorization = authHeader;
string json = JsonSerializer.Serialize<DailyHours>(item, serializerOptions);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine(@"\tTodoItem successfully saved.");
}
}
catch (Exception ex)
{
Debug.WriteLine(@"\tERROR {0}", ex.Message);
}
}
我的 AddPage 看起来像:
async void OnSaveButtonClicked (object sender, EventArgs e)
{
var todoItem = (DailyHours)BindingContext;
await App.DailyHoursManager.SaveTaskAsync(todoItem);
await Navigation.PopAsync ();
}
所以我的问题是,我的 Xaml 文件应该是什么样子,我应该改变什么来做我的帖子,如下所示,在 JSON 中看起来像这样:
{
"daily_montage": [
{
"status": "",
"time_start": "",
"end_time": "",
"daily_hours": "",
"date": "",
"name":1
},
{
"status": "",
"time_start": "",
"end_time": "",
"daily_hours": "",
"date": "",
"name":2
},
],
"type": null,
"day_montage": null,
"date": null,
"user": null,
"team": null
}
解决方案
推荐阅读
- python - 为什么除了 NameError 没有在我的代码中提取名称错误?
- amazon-ec2 - 在没有 SSH 密钥的情况下从 gitlab runner 在 ec2 实例上运行 ansible?
- jquery - 在我的结帐中实施了 Stripe v3 卡片元素,并且 jquery 停止工作
- typescript - 与泛型函数一起使用的接口的默认值
- firebase - 我可以使用自定义声明设置一个名为“已禁用”的标志吗?
- mysql - 在非主键列上分区 mysql 表
- javascript - jQuery UI 自动完成中的“值”与“标签”
- gradle - 丢失了 gradle 构建扫描链接
- javascript - 服务器端“JSON 输入意外结束”错误
- javascript - 如何使用 jQuery 在多个非文本节点上选择文本