mvc4 @html.textbox,c# - Validation of @Html.Textbox in MVC4 - Stack Overflow

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");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值