-1

Trying to add information about a selected card from an API to a table in my database, so I can print that information out. I've tried adding the information to card list but it does not accept the parameters I am trying.

            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");
                }
            }

So far this just displays the information from the API and formats that to the model.

My model

     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; }
        }

My View

    @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>
Dale K
  • 25,246
  • 15
  • 42
  • 71
dros
  • 1,217
  • 2
  • 15
  • 31
  • i'm not understanding your problem, when you deserialize the json, the CardsInfo has the information you want ?! Other thing is you want to add data to your database throw EF but i dont see in your code any method that performs that insertion in the database. – Pedro Brito Sep 12 '19 at 09:53
  • Hi there, you are exactly right. I am unsure of how to create the method that performs that insertion into the database. I've deserialized it enough to see it, just don't know how to stick it in my EF database! (new to EF) and databases in general – dros Sep 12 '19 at 09:55

2 Answers2

-1

Your above code Just retrieve information as GET Method.

if your want send something from client to Api use POST Method to Send Data.

Here is Example.. --> Click Here

I hope this will help you..

Mohideen Asraf
  • 32
  • 1
  • 1
  • 8
  • I am trying to make a post request from the api, to my databse. So i'm getting the information with the above request found in the controller. I'm just unsure about how exactly to set that information into my database, more so i am unsure of the tools to use and the process to follow. – dros Sep 12 '19 at 09:51
-1

Here you have an example to save in the database

    public async Task<"Anything"> InsertCard(Card card)
    {
         await _dbContext.AddAsync(card);
          _dbContext.SaveChanges();
         return "Anything";
    }

Example:

     public async Task<bool> InsertCard(Card card)
    {
        try
        {
            await _dbContext.AddAsync(card);
            _dbContext.SaveChanges();

            return true;
        }
        catch
        {
            return false;
        }

    }

Anything-> Edit to return bool, or int(card Id) or Card For more information about EF Core and CRUD operations, see this http://www.codaffection.com/asp-net-core-tutorial/asp-net-core-mvc-crud-with-ef-core/

Pedro Brito
  • 263
  • 1
  • 9
  • `Task<"Anything">` does not compile – trailmax Sep 12 '19 at 10:11
  • is for you to add the type you want, bool or int our return the card is for you to edit. – Pedro Brito Sep 12 '19 at 10:25
  • here you can just use `public async Task InsertCard(...)` because you are not really returning any object. – trailmax Sep 12 '19 at 10:28
  • you can return a bool imagine that you have an exception, for you to know if the insertion was successful or not – Pedro Brito Sep 12 '19 at 10:30
  • Yes, you can. But no point in providing a sample here that does not compile or makes sense. – trailmax Sep 12 '19 at 10:33
  • Edited my answer, the sample that i posted was for you to analyse and modify the return type because i dont know the return type that is more useful for you. The return type is necessary for you to know if the insertion was successful or not in the client side. – Pedro Brito Sep 12 '19 at 10:35
  • This actually helped me progress a fair bit forward, i can actually submit something to the database but its returning an empty object! – dros Sep 12 '19 at 12:54
  • Ok, edit your post with the new code, to understand the problem. And please mark my answer as useful because was indeed was useful – Pedro Brito Sep 12 '19 at 13:33