如何将具有输入值的对象传递给控制器​​发布方法

时间:2019-07-26 01:51:25

标签: c# html razor asp.net-core-mvc

我创建了一个表格形式的项目表,每个表格旁边都有用于订购的按钮。对于每个项目,我希望用户在提交之前指定数量。单击订购按钮后,我需要该项目以及要发送到控制器post方法的数量参数,在该方法中,我将该数据发送到Sql Server进行操作(我已经用Sql编写了存储过程)。我在弄清楚如何从视图中将ItemId和InputQty发送到控制器post方法时遇到了麻烦。

//型号

public class USA
    {
        [Key]
        public int ItemId { get; set; }
        public int PlanSeqId { get; set; }
        public string ItemDescription { get; set; }
        public int OrderQuantity { get; set; }
        public int OrderSldTdy { get; set; }
        public int PlannedMinutesQty { get; set; }
        public int ActualMinutesQty { get; set; }
        public int NetworkId { get; set; }
        public int CompanyId { get; set; }
        public int AvaiForSaleQty { get; set; }
        [DataType(DataType.Date)]
        public DateTime ShowDate { get; set; }
        public string ShowCd { get; set; }
        [NotMapped]
        public int InputQty { get; set; } 
    }
}

//控制器

private readonly OrderContext _context;

        public USAsController(OrderContext context)
        {
            _context = context;
        }

        // GET: USAs
        public async Task<IActionResult> Index()
        {
            return View(await _context.USA.ToListAsync());
        }

[HttpPost]
        public IActionResult Order(USA usa)
        {          

            string path = Directory.GetCurrentDirectory();
            var builder = new ConfigurationBuilder()
                .SetBasePath(path)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            IConfiguration config = builder.Build();
            string sqlConnectionString = config.GetConnectionString("OrderContext");
            string sql = "OrderItemUpdate";            

            if (usa.AvaiForSaleQty != 0 && usa.AvaiForSaleQty - usa.OrderQuantity >= 0)
            {
                using (var connection = new SqlConnection(sqlConnectionString))
                {
                    try
                    {
                        connection.Open();
                        DynamicParameters parameter = new DynamicParameters();
                        parameter.Add("@CompanyId", usa.CompanyId, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@ItemId", usa.ItemId, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@PlanSeqId", usa.PlanSeqId, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@ItemDescription", usa.ItemDescription, DbType.String, ParameterDirection.Input);
                        parameter.Add("@OrderQuantity", usa.OrderQuantity, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@OrderSldTdy", usa.OrderSldTdy, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@PlannedMinutesQty", usa.PlannedMinutesQty, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@ActualMinutesQty", usa.ActualMinutesQty, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@NetworkId", usa.NetworkId, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@AvaiForSaleQty", usa.AvaiForSaleQty, DbType.Int32, ParameterDirection.Input);
                        parameter.Add("@ShowDate", usa.ShowDate, DbType.String, ParameterDirection.Input);
                        parameter.Add("@ShowCd", usa.ShowCd, DbType.String, ParameterDirection.Input);
                        connection.Execute(sql, parameter, commandType: CommandType.StoredProcedure);
                        return RedirectToAction(nameof(Index));
                    }
                    catch (SqlException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    connection.Close();
                }
                return View();
            }
            else
            {
                ViewBag.Message("Inventory too low for amount ordered.");
                return View();
            }
        }

//查看

@model IEnumerable<QxHOrderSystem.Models.USA>

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<form asp-action="Order" method="post" class="form">
    <table class="table">
        <thead>
            <tr>
                <th></th>
                <th>
                    @Html.DisplayNameFor(model => model.ItemId)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ItemDescription)
                </th>
                <th>
                    Order Quantity
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.AvaiForSaleQty)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>                        
                        <input type="submit" value="Order" />                        
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ItemId)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ItemDescription)
                    </td>
                    <td>
                        <input asp-for="@item.InputQty" class="form-control" value="" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.AvaiForSaleQty)
                    </td>
                    <td>
                        <a asp-action="Edit" asp-route-id="@item.ItemId">Edit</a> |
                        <a asp-action="Details" asp-route-id="@item.ItemId">Details</a> |
                        <a asp-action="Delete" asp-route-id="@item.ItemId">Delete</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
</form>

有没有一种方法可以不使用jQuery?我并不反对,但是我觉得好像有一种简单的方法,就是我没有看到使用HTML / C#。预先感谢。

1 个答案:

答案 0 :(得分:0)

似乎您只想将单个实体发布到服务器端,可以修改以下代码:

@model List<QxHOrderSystem.Models.USA>
<table class="table">
    <thead>
        <tr>
            <th></th>
            <th>
                ItemId
            </th>
            <th>
                ItemDescription
            </th>
            <th>
                Order Quantity
            </th>
            <th>
                AvaiForSaleQty
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.Count(); i++)
        {
            using (Html.BeginForm("Order", "Home", FormMethod.Post))
            {
                <tr>
                    <td>
                        <input type="submit" value="Order" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => Model[i].ItemId)
                        <input type="hidden" value="@Model[i].ItemId"  name="ItemId"/>
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => Model[i].ItemDescription)
                    </td>
                    <td>
                        @Html.EditorFor(model => Model[i].InputQty, null, "InputQty")
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => Model[i].AvaiForSaleQty)
                    </td>
                    <td>
                        <a asp-action="Edit" asp-route-id="@Model[i].ItemId">Edit</a> |
                        <a asp-action="Details" asp-route-id="@Model[i].ItemId">Details</a> |
                        <a asp-action="Delete" asp-route-id="@Model[i].ItemId">Delete</a>
                    </td>
                </tr>
            }
        }

    </tbody>

在服务器端:

public IActionResult Order(USA usa)
{
   .....
}

当点击ItemId按钮时,它将发送InputQtyOrder