【问题标题】:How I can pass data in view using Post method and ViewBag?如何使用 Post 方法和 ViewBag 在视图中传递数据?
【发布时间】:2020-05-14 11:29:02
【问题描述】:

第一次查看发送表单

foreach p in Model
<form method="post">
 <td class="pricelight"><input type="radio" name="price"value="@p.PriceLight.ToString("")"/>@p.PriceLight.ToString("")</td>
        <td class="fromtabletime"><input type="radio" name="price"value="@p.PriceOk.ToString("")/>@p.PriceOk.ToString("")</td>
        <td class="totable"><input type="radio" name="price"value="@p.PriceHigh.ToString("")/>@p.PriceHigh.ToString("")</td>
         <td class="button13"><button type="submit" asp-action="Buy" asp-route-id="@p.PhoneId" asp-controller="Home">Next</button></td>
</form>

然后我需要在 post 中处理数据 控制器

       [HttpPost]
        public ActionResult Buy(int?id,Order order,string price,@p.PriceLight,@p.PriceOk,@p.PriceHigh)
        {
            string ryt={price};
             ViewBag.price=ryt;
            context.Orders.Add(order);

            context.SaveChanges();
            ViewBag.phoneId=id;
            return View("Thanks, " + order.OrderName);


        }

然后我想在新视图中获取它

@using System.Linq;
@using System;


@model List<Searchphone>



@{
    ViewBag.Title = "PhoneId";
}


<h2 class="orderinformation">Order Data</h2>
<form method="post" class="formpass">
       <h3> <input type="hidden" name="@ViewBag.price" value="@ViewBag.price" />@ViewBag.price</h3>
    <br>
    <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>

1) 我没有看到它,只有在 URL 中。如何使用 Post 方法将其显示在视图中?为什么 viewbag 不起作用? 2) 我想从 post 方法的第一个视图中将价格值与 @p.PriceLight 进行比较。但是我如何在方法中传递@p.PriceLight 呢?

【问题讨论】:

标签: asp.net-mvc asp.net-core


【解决方案1】:

1) 我没有看到它,只有在 URL 中。如何使用 Post 方法将其显示在视图中?为什么 viewbag 不起作用?

您应该首先调试以检查 ViewBag 在您的控制器操作中是否有值。由于 ViewBag 用于隐藏输入,因此无法直接在页面上看到它,您可以在视图中按 f12 来检查 ViewBag 是否被渲染。我使用了你的代码,并且可以在视图中获取它。

2) 我想从 post 方法的第一个视图中将价格值与 @p.PriceLight 进行比较。但是我如何在方法中传递@p.PriceLight 呢?

表单只提交选中的单选按钮值,如果你想获取PriceLight的值,在你的第一个视图中定义一个隐藏字段。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多