首页 > 解决方案 > 具有多个模型的 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

}

标签: c#xamarinxamarin.forms

解决方案


推荐阅读