如何从Asp.Net Mvc中的表单传递数据

时间:2017-12-06 15:04:44

标签: asp.net-mvc html.textboxfor

我想将值TextBoxFor传递给我的更新操作,但我不知道怎么做。我知道我必须通过这样的地方......新的{$ id ="状态"}。 我必须按钮一个用于增加,一个用于减少..但主要问题是将Textbox的值传递给我的动作.. 这是我的InventoryUpdateDto类和Action

public class InventoryUpdateDto 
    {
        public int InvId { get; set; }

        [RegularExpression("^[0-9]*$", ErrorMessage = "Status must be numbers")]
        [Required(AllowEmptyStrings = false)]
        public int Status { get; set; }
    }

[HttpPost]
        public async Task<ActionResult> Increase(InventoryUpdateDto inventory)
        {
             using (context)
            {
                if (!ModelState.IsValid)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                    var prRecord = await context.Inventories.FindAsync(inventory.InvId);
                    prRecord.Status = prRecord.Status + inventory.Status;
                    context.Inventories.Add(prRecord);
                    context.Entry(prRecord).State = EntityState.Modified;
                    await context.SaveChangesAsync();
                    SllitHub.NotifyCurrentInformationToAllClients();
                    return RedirectToAction("Index");
            }

         }

我的表格:

@using (Html.BeginForm())
        {
            @Html.HiddenFor(model => model.InvId)

            <div class="well well-sm col-lg-6">
                // How can I send value of this TextBox value to my action?
                // I tried but could not update
                @Html.TextBox("Status", null, new { @class = "form-control" })
                // This one is better I can Update but it's not empty textbox
                @*@Html.TextBoxFor(model => model.Status, new { @class = "form-control" })*@
                @Html.ValidationMessageFor(model => model.Status)

                <input type="submit" name="response" value="Increase" formaction=@Url.Action("Increase", "Home") formmethod="post"/>
                <input type="submit" name="response" value="Decrease" formaction=@Url.Action("Decrease", "Home") formmethod="post"/>
            </div>

        }

我也是这样尝试的

@Html.TextBox("Status", null, new { @class = "form-control", @id = "Status" })

提前谢谢!

0 个答案:

没有答案