c# - 在 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 - 如何使用开关来偏移 for 循环的开始?
- web-scraping - 如何在本地更改没有 VPN/代理的 IP 地址?
- node.js - Heroku Redis 使用 nodejs 开发
- concurrency - 与 Ben Ari 的频道就餐哲学家陷入僵局?
- c++ - 如何调用在一个类中创建然后传递给另一个类的 c++ 函数指针?
- javascript - 将 HTML 表单提交到带有 JS 验证的 PHP 文件
- python - 有什么方法可以改变 PyGame 中标题的颜色吗?
- delphi - Delphi,表单发布,阻止Application.MessageBox执行
- maven - Flyway 在 Maven 多模块项目中找不到迁移
- swift - 为 UserDefaults 发布者编译安全而不是崩溃?