【发布时间】: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 呢?
【问题讨论】:
-
ViewBag 只持续一个请求。一种选择是使用post/redirect/get pattern。