更新アクションを使用して、@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)
<input type="submit" value="Update" />
}
と私のモデルクラスで
[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; }
問題は、テキストボックスが空のときに検証メッセージが表示されないことです。しかし、@Html.TextBoxForを使用すると
@Html.TextBoxFor(modelItem => item.Quantity)
@Html.ValidationMessageFor(modelitem => item.Quantity)
検証メッセージが表示されます。更新アクションが機能していません。
ここに2つのオプションがあります。
1.@Html.TextboxForでテキストボックス名「qty」を渡す方法は?? (または)
2。@ Html.ValidationMessage()を使用して@ Html.Textbox()で検証メッセージを取得する方法
助言がありますか ..
編集:私の更新アクション
[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");
}