How to pass data from form in Asp.Net Mvc

537 Views Asked by At

I want to pass value TextBoxFor to my update action but I don't get it how to do. I know I have to pass somewhere maybe like this... new { $id = "Status"}. I have to buttons one for Increase and one for Decrease.. But the main problem is to pass a value of Textbox to my action .. Here is my InventoryUpdateDto class and 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");
            }

         }

And my form:

@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" [email protected]("Increase", "Home") formmethod="post"/>
                <input type="submit" name="response" value="Decrease" [email protected]("Decrease", "Home") formmethod="post"/>
            </div>

        }

I tried like this too

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

Thank you in advance!

0

There are 0 best solutions below