首页 > 解决方案 > 在 C#.Net 和实体框架中将数据从 API 添加到数据库?

问题描述

尝试将有关所选卡片的信息从 API 添加到我的数据库中的表中,以便我可以打印出该信息。我尝试将信息添加到卡列表,但它不接受我正在尝试的参数。

            public async Task<ActionResult> AddCard()
            {
                List<Card> CardsInfo = new List<Card>();

                var getUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

                var test = new Uri(getUrl);

                var id = test.Segments.Last();

                SingleResult += id;

                using (var client = new HttpClient())
                {
                    //passing service baseurl 
                    client.BaseAddress = new Uri(SingleResult);

                    client.DefaultRequestHeaders.Clear();

                    //Define request data format
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //Sending request to find web api REST service resource Getallcards using HTTPClient
                    HttpResponseMessage Res = await client.GetAsync(SingleResult);

                    //Checking the response is successful or not which is sent using HttpClient  

                    if (Res.IsSuccessStatusCode)
                    {
                        //Storing the response details received from web api

                        var CrdResponse = Res.Content.ReadAsStringAsync().Result;


                        //Deserializing the response recieved from web api and storing in to the card list
                        CardsInfo = JsonConvert.DeserializeObject<List<Card>>(CrdResponse);

                    }

                    //returning the card list to view
                    return View("Index");
                }
            }

到目前为止,这只是显示来自 API 的信息并将其格式化到模型中。

我的模型

     public class Card
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int? Atk { get; set; }
            public int? Def { get; set; }
            public string Desc {get; set;}
            public int? Level { get; set; }
            public string Type { get; set; }
            public string Attribute { get; set; }
            [DisplayName("Image")]
            public IList<Image> Card_Images { get; set; }

            public IList<Deck> Deck { get; set; }
        }

我的观点

    @model IEnumerable<YGOBuilder.Models.Card>

    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Atk)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Def)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Type)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Attribute)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Level)
            </th>
            <th></th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Atk)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Def)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Attribute)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Level)
            </td>
            <td>
                @Html.ActionLink("Details", "Details", new { id=item.Id })
            </td>
        </tr>
    }

</table>

标签: c#.net

解决方案


您上面的代码只需以 GET 方法检索信息。

如果您想从客户端向 Api 发送某些内容,请使用 POST 方法发送数据。

这是示例.. -->单击此处

我希望这能帮到您..


推荐阅读