asp.net从几个textFields中获取值

时间:2018-08-14 09:04:38

标签: asp.net-mvc

我在收集文本字段中的值时遇到问题。 在屏幕的每一行中,我都有一个文本字段,用户应该在其中添加值(您可以从图片中看到它)

exaple of my site

这是我的html代码:

@using (Html.BeginForm("Create", "User", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.BarCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Profit)
            </th>
            <th>
                Amount
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.BarCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Profit)
                </td>
                <td>
                    <form action="/action_page.php">
                        Amount:
                        <input type="text" id="amountTag" name="Amount" maxlength="2" placeholder="0" size="4" runat="server" />
                    </form>
                </td>
            </tr>
        }
        <tr>
            <td><input type="submit" value="Submit" class="btn btn-default" /></td>
        </tr>

    </table>

}
我的代码是

  // POST: User/Create
    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {

            /*Update the Database*/
            // TODO: Add insert logic here
            string x = Request.Form["amountTag"];
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

此代码始终为Null,我不知道为什么

1 个答案:

答案 0 :(得分:0)

本节将在现有表单(嵌套表单)中创建一个表单,这不是绑定文本框的好方法:

<form action="/action_page.php">
    Amount:
    <input type="text" id="amountTag" name="Amount" maxlength="2" placeholder="0" size="4" runat="server" />
</form>

正确的方法应该是这样的:

1)在与intAmountBarCode属性相同的视图模型类中,创建Name的{​​{1}}属性。

Profit

2)使用public class ViewModelName { // required unique key for each rows public int Id { get; set; } // other 3 properties here [DisplayName("Amount")] public int Amount { get; set; } } 循环将视图内部的模型与HTML帮助器绑定。在这里,您应该为for属性添加TextBoxFor帮助器,以使用户输入具有以前拥有的HTML属性的数字值:

Amount

3)确保用@model IEnumerable<ViewModelClassName> @using (Html.BeginForm("Create", "User", FormMethod.Post)) { @Html.AntiForgeryToken() <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.BarCode) </th> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Profit) </th> <th> Amount </th> </tr> @for (int i = 0; i < Model.Count; i++) { @Html.HiddenFor(modelItem => modelItem[i].Id) <tr> <td> @Html.DisplayFor(modelItem => modelItem[i].BarCode) </td> <td> @Html.DisplayFor(modelItem => modelItem[i].Name) </td> <td> @Html.DisplayFor(modelItem => modelItem[i].Profit) </td> <td> @* Amount *@ @Html.LabelFor(modelItem => modelItem[i].Amount) @Html.TextBoxFor(modelItem => modelItem[i].Amount, new { maxlength = 2, ... }) </td> </tr> } <tr> <td><input type="submit" value="Submit" class="btn btn-default" /></td> </tr> </table> } 装饰的action方法具有viewmodel类列表作为参数。

HttpPostAttribute

此时,您的模型绑定应该可以正常工作。

相关问题:Need to pass a List<model> to the Http Post in a Controller