I use an update action to update based on the input from the @Html.Textbox.
@using (Html.BeginForm("Update", "Shopping", new { UserID = Request.QueryString["UserID"] }, FormMethod.Post, new { id = "myForm" }))
{
@Html.ValidationSummary()
@Html.Hidden("id", @Request.QueryString["UserID"] as string)
@Html.Hidden("productid", item.ProductID as string)
@Html.TextBox("Quantity", item.Quantity)
@Html.ValidationMessage("Quantity", "*")
@Html.Hidden("unitrate", item.Rate)
}
and In My Model class
[Required(ErrorMessage = "Quantity is required.")]
[Display(Name = "Quantity")]
[Range(2, 100, ErrorMessage = "There is not enough inventory for the product to fulfill your order.")]
public int? Quantity { get; set; }
The problem is I m not getting the validation message when the textbox is empty.
But when I use @Html.TextBoxFor
@Html.TextBoxFor(modelItem => item.Quantity)
@Html.ValidationMessageFor(modelitem => item.Quantity)
I am getting the validation message. and my update action is not working.
Here I have two options.
1. How to pass the textbox name "qty" in @Html.TextboxFor ?? (or)
2. How to get the validation message in @Html.Textbox() using @Html.ValidationMessage()
Any suggestions ..
EDIT :
My Update Action
[HttpPost]
public ActionResult Update(string id, string productid, int Quantity, decimal unitrate)
{
if (ModelState.IsValid)
{
int _records = UpdatePrice(id, productid, Quantity, unitrate);
if (_records > 0)
{
return RedirectToAction("Index1", "Shopping", new { UserID = Request.QueryString["UserID"] });
}
else
{
ModelState.AddModelError("","Can Not Update");
}
}
return View("Index1");
}