首页 > 解决方案 > 如何从get中获取数据并将其发布?手柄输入电台类型

问题描述

表单发送get请求,得到如下视图

@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>

所有数据都是从多个链接表生成的。假设用户选择“价格”之一,表单必须在 Post 请求中传递选择的数据和价格。

2)我是这样做的

@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="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>



    </tr>

    }
</table>
</form>

当我需要去下一个表格时。我做了下一个视图

@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>

控制器

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


        }

但我有问题。我得到了 URL:~/Home/Buy/0 和返回“谢谢”的视图。我没有得到输入订单数据的视图。数据库中的表顺序获取默认值和空值。我的错误是什么?

第 2 部分。添加到下一个问题

控制器

 [HttpGet]
        public IActionResult Buy(int? id, string price)
        {
            if (id == null) return RedirectToAction("Index");
            if(price=="PriceLight.ToString("")")
            ViewBag.price=PriceLight.ToString("");
            if(price=="PriceOk.ToString("")")
            ViewBag.price=PriceOk.ToString("");
             if(price=="PriceHigh.ToString("")")
            ViewBag.price=PriceHigh.ToString("");
            ViewBag.PhoneId = id;
            return View();
        }

看法

@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>

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

解决方案


表单在 post 方法中提交,而返回视图操作仅接受 HttpGet 请求。因此,它总是会触发 HttpPost 操作,因为它们具有相同的名称。

我测试了您的代码,发现您的第一个视图存在一些问题。

[HttpGet]
  public IActionResult Buy(int? id)
  {
      if (id == null) return RedirectToAction("Index");
      ViewBag.PhoneId = id;
      return View();
  }

这个动作只接收一个 id 参数。无需提交整个表格。在我看来,您甚至不需要使用表单,只需使用 < a > 标签,如下所示:

@using System.Linq;
@model List<Searchphone>

<h2 class="search1"></h2>

@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="totable"> <a asp-action="Buy" asp-route-id="@p.PhoneId" asp-controller="Home">Next</a></td>
        </tr>
    </table>

}

推荐阅读