locked
How to take data from get and put it to post?Getting default values as id(((( RRS feed

  • Question

  • The form sends a get request and gets the following view

    @using System.Linq;
    @model List<Searchphone>
    
    
    <h2 class="search1"></h2>
     <form class="priceeconom1" method="post" asp-action="Index" asp-controller="Home">
    
    
        @foreach (var p in Model)
    <table>
    
        {
    
    
        <tr class="searchdata">
            <td class="flight">@p.ColourPhone</td>
            <td class="fromtable">@p.TypePhone</td>
            <td class="fromtable">@p.SizePhone</td>
            <td class="fromtable">@p.PriceLight</td>
            <td class="fromtabletime">@p.PriceOk</td>
            <td class="totable">@p.PriceHigh</td>
    
    
        </tr>
    
        }
    </table>

    All data is generated from multiple linked tables.It is assumed that the user selects one of the "Price" and the form must pass the selected data and price in the Post request.

    2)I did it like this

    @using System.Linq;
    @model List<Searchphone>
    
    
    <h2 class="search1"></h2>
    
      <form class="priceeconom1" method="post" asp-action="Index" asp-controller="Home">
    
        @foreach (var p in Model)
    <table>
    
        {
    
    
        <tr class="searchdata">
            <td class="flight"><input type="text" name="@p.ColourPhone</td>
            <td class="fromtable"><input type="text" name="@p.TypePhone</td>
            <td class="fromtable"><input type="text" name="@p.SizePhone</td>
            <td class="pricelight"><input type="checkbox" name="@p.PriceLight.ToString("")"/></td>
            <td class="fromtabletime"><input type="checkbox" name="@p.PriceOk.ToString("")/></td>
            <td class="totable"><input type="checkbox" name="@p.PriceHigh.ToString("")/></td>
             <td class="button13"><button type="submit" asp-action="Buy" asp-route-id="@p.PhoneId" asp-controller="Home">Next</button></td>
    
    
    
        </tr>
    
        }
    </table>
    </form>

    When I need to go to the next post-form. And I did the next view

    @using System.Linq;
    @using System;
    
    
    @model List<Searchphone>
    
    
    
    @{
        ViewBag.Title = "PhoneId";
    }
    
    
    <h2 class="orderinformation">Order Data</h2>
    <form method="post" class="formpass">
        <input type="hidden" id="PhoneId"value="@ViewBag.PhoneId" name="PhoneId">
        <label for="OrderSurName">Surname</label><br>
        <input type="text" placeholder="Enter Surname" name="OrderSurName" required><br>
    
        <label for="OrderName">Имя</label><br>
        <input type="text" placeholder="Enter Name" name="OrderName" required><br>
        <button type="submit" class="button7">To pay</button>
    
    </form>
    Controller
     [HttpGet]
            public IActionResult Buy(int? id)
            {
                if (id == null) return RedirectToAction("Index");
                ViewBag.PhoneId = id;
                return View();
            }
            [HttpPost]
            public string Buy(Order order)
            {
                context.Orders.Add(order);
    
                context.SaveChanges();
                return "Thanks, " + order.OrderName;
    
    
            }

    But I have problem. I got URL:~/Home/Buy/0 and the view with return "Thanks, ". I don`t get the view for input Order Data. Table Order in Database gets default and null values. What is my mistake?
    • Edited by Forkin Thursday, May 7, 2020 4:50 PM
    • Moved by CoolDadTx Friday, May 8, 2020 1:22 PM ASP.NET related
    Thursday, May 7, 2020 4:47 PM

All replies

  • Please post questions related to Web API in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, May 8, 2020 1:22 PM