Не передается информация в контролер

Есть ссылка с информацией

@foreach (var item in Model)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Price)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Count)
    </td>
    <td>
    <td><a href="~/Home/Buy/@item.Price">Купить</a></td>

    </td>

Есть метод его принимающий

[HttpGet]
public IActionResult Buy(decimal? Price)
{
    if(Price == null)
    {
        RedirectToAction("Index");
    }
    ViewBag.Price = Price;

    return View();
}

[HttpPost]
public IActionResult Buy(decimal price ,decimal name)
{
    ViewBag.Sum = price + name;

    return View();
}

Это представление Buy

@{
    ViewData["Title"] = "Buy";
}
<form method="post">
    <input type="hidden" value="@ViewBag.Price" name="Price" />

    <table>
        <tr>
            <td>Сумма:</td>
            <td><input type="number" name="Name" /> </td>
        </tr>

        <tr><td><input type="submit" value="Отправить" /> </td><td></td></tr>
    </table>

</form>
<p> Rezult:  @ViewBag.Sum </p>

Но почему-то Price вместо определенной цены выводится 0. Не могу понять, почему не выводит информацию с ViewBag.Price.


Ответы (2 шт):

Автор решения: Александр Писковой

Протестировал ваш код, но вместо <a href="~/Home/Buy/@item.Price">Купить</a> использовал <a href="~/Home/Buy/100">Купить</a> и всё корректно работало. Возможно проблема здесь:

@foreach (var item in Model)

Попробуйте явно типизировать переменную item.

→ Ссылка
Автор решения: Iskhulukhia23 Fg

Изменил <a href="~/Home/Buy/@item.Price">Купить</a>

на <a href="~/Home/Buy/@item.Id">Купить</a>

в методе Buy добавил

       {
         if(id==null)
           {
               RedirectToAction("Index");
           }
           var Price = db.Products.Where(a => a.Id == id).FirstOrDefault().Price;

           ViewBag.Price = Price;
           return View();
       }```

а в самом представлении

```<input type="hidden" value="@ViewBag.Price" name="Price" />```
внес внутрь таблицы
→ Ссылка